25.文件a1.txt内容(升级题)
name:apple price:10 amount:3 year:2012
name:tesla price:100000 amount:1 year:2013
通过代码,将其构建成这种数据类型:
[{'name':'apple','price':10,'amount':3},
{'name':'tesla','price':1000000,'amount':1}......]
并计算出总价钱。
lst = []
sum = 0
with open('a','r',encoding='utf-8') as f1:
for line in f1:
dic = {}
line_lst = line.strip().split(' ')
for j in line_lst:
key,value = j.split(':')
dic.setdefault(key,value)
lst.append(dic)
print(lst)
for i in lst:
sum += int(i['price']) * int(i['amount'])
print(sum)