python文件例题_文件操作练习题

'''

1. 文件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}......]

并计算出总价钱。

'''

list_info = []

with open(r'b.txt','r',encoding='utf-8') as f:

for i in f:

list = i.split()

print(list)

list_info.append({'name':list[0],'price':int(list[1]),'count':int(list[2])})

# money = list(map(lambda x:x['price']*x['count'],list_info))

money = map(lambda x: x['price'] * x['count'], list_info)

ss = sum(money)

print('共消费 %d

你可能感兴趣的:(python文件例题)