标签:编号 水果 价格 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