2 回答
TA贡献1864条经验 获得超2个赞
下面(“信息”下的数据是一个列表,您需要迭代列表条目)
data = [
{'Name': 'Sneaker shoes', 'Date & Time': '2019-12-03 18:21:26',
'Information': ['Sizes from 38 to 44', 'Comes in 5 different colour', 'Rated 4.3 out of 5 stars']},
{'Name': 'Worker boots', 'Date & Time': '2018-11-05 10:12:15',
'Information': ['Sizes from 38 to 44', 'Comes in 2 different colour', 'Rated 3.7 out of 5 stars']},
{'Name': 'Slippers', 'Date & Time': '2018-10-05 13:15:44',
'Information': ['Sizes from 38 to 42', 'Comes in 3 different colour', 'Rated 4.1 out of 5 stars']}
]
with open("shoes_db.txt", 'w') as f:
for idx, entry in enumerate(data):
f.write(entry['Name'] + '\n')
f.write(entry['Date & Time'] + '\n')
for info in entry['Information']:
f.write(info + '\n')
if idx != len(data) - 1:
f.write('\n')
TA贡献2041条经验 获得超4个赞
with open("shoes_db.txt", 'w') as f:
for entry in data:
f.write(entry['Name'] + '\n')
f.write(entry['Date & Time'] + '\n')
添加回答
举报