码迷,mamicode.com
首页 > 其他好文 > 详细

水果统计

时间:2018-12-05 18:39:41      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:编号   水果   价格   txt   spl   名称   title   字典   \n   

f = open("水果.txt", mode="r", encoding="utf-8")

lst = []
for line in f:  # 1,香蕉,1.85,50
    dic = {}  # 每行都是一个字典
    line = line.strip()  # 去掉空白  \n 1,香蕉,1.85,50
    a, b, c, d = line.split(",") # [1, 香蕉, 1.85, 50]
    dic[‘id‘] = a
    dic[‘name‘] = b
    dic[‘price‘] = c
    dic[‘totle‘] = d
    lst.append(dic)

print(lst)





#
# f = open("水果.txt", mode="r", encoding="utf-8")
# line = f.readline().strip() # 第一行内容,   编号,名称,价格,数量,哈哈
# h,i,j,k = line.split(",")
# lst = []
# for line in f:  # 1,香蕉,1.85,50
#     dic = {}  # 每行都是一个字典
#     line = line.strip()  # 去掉空白  \n 1,香蕉,1.85,50
#     a, b, c, d = line.split(",") # [1, 香蕉, 1.85, 50]
#     dic[h] = a
#     dic[i] = b
#     dic[j] = c
#     dic[k] = d
#     lst.append(dic)
#
# print(lst)



f = open("水果.txt", mode="r", encoding="utf-8")
line = f.readline().strip() # 第一行内容,   编号,名称,价格,数量,哈哈
title = line.split(",") # [编号,名称,价格,数量,哈哈]
lst = []
i = 0

for line in f:  # 1,香蕉,1.85,50
    dic = {}  # 每行都是一个字典
    line = line.strip()  # 去掉空白  \n 1,香蕉,1.85,50
    data = line.split(",") # [1, 香蕉, 1.85, 50]

    for i in range(len(title)):
        dic[title[i]] = data[i]

    lst.append(dic)

print(lst)

  

水果统计

标签:编号   水果   价格   txt   spl   名称   title   字典   \n   

原文地址:https://www.cnblogs.com/WANG-/p/10072338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!