1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数。

li = []
with open('lo', encoding='utf-8', mode='r') as f1:
for i in f1:
l2=i.strip().split()
dic = {'name':l2[0], 'price':l2[1], 'amount':l2[2]}
li.append(dic)
print(li)
sum = 0
for j in li:
sum=sum+int(j['price'])*int(j['amount'])
print(sum)

[{'name': 'apple', 'price': '10', 'amount': '3'}, {'name': 'tesla', 'price': '100000', 'amount': '1'}, {'name': 'mac', 'price': '3000', 'amount': '2'}, {'name': 'lenovo', 'price': '30000', 'amount': '3'}, {'name': 'chicken', 'price': '10', 'amount': '3'}]
196060

posted @ 2018-04-19 22:57  dongyanyan  阅读(1279)  评论(0编辑  收藏  举报