水果统计

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)

  

posted @ 2018-12-05 17:49  =-=-  阅读(144)  评论(0编辑  收藏  举报