文件练习题

#  文件a.txt内容:每一行内容分别为商品名字,价钱,个数。
#
# apple 10 3
#
# tesla 100000 1
#
# mac 3000 2
#
# lenovo 30000 3
#
# chicken 10 3
#
# 通过代码,将其构建成这种数据类型:[{'name':'apple','price':10,'amount':3},{'name':'tesla','price':1000000,'amount':1}......] 并计算出总价钱。
#
#
# list1 = []
# with open('a1.txt',mode='r',encoding='utf-8') as f:
# for line in f.readlines():#可以直接f
# lines = line.split()
# list1.append({'name':lines[0],'price':int(lines[1]),'amount':int(lines[2])})
# ll = list(map(lambda x:x['price']*x['amount'],list1))#将每个种类的价钱乘以数量
# ss = sum(ll)#求和
# print(ll)
# print(ss)
# print(list1)
#
# # (如果有空行,应先去掉空行,重新写入一个新的文件)
# # 首先将文件循环到每一行,进行分割成列表,
# # 再将每一个列表形式的行添加到一个新的以字典值为列表索引取值的列表中
#