简体   繁体   English

Python - 为我的字典列表写入 txt 文件

[英]Python - Write into txt file for my list of dictionaries

Suppose I have the list of dictionaries dataset that is stored like this:假设我有这样存储的字典数据集列表:

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']}
]

I been trying to write the data into the txt file, but I couldn't figure out how to code it out.我一直在尝试将数据写入 txt 文件,但我不知道如何对其进行编码。 Here's what I did这是我所做的

shoes_database = open("shoes_db.txt", 'w')
for value in data:
    shoes_database.write('\n'.join([value.get('Name'), str(value.get('Date & Time')), str(value.get('Information')), '\n']))
shoes_database.close()

For the values in 'Name' and 'Date & Time' are stored properly into the file.因为“名称”和“日期和时间”中的值已正确存储到文件中。 However, for the 'Information' I am having trouble to store each of the shoes information into a new line and without the square brackets and '' as it is a list.但是,对于“信息”,我无法将每条鞋子信息存储到一个新行中并且没有方括号和“”,因为它是一个列表。

Here is what I would like to write and store in my shoes_db.txt file这是我想写入并存储在我的 shoes_db.txt 文件中的内容

Sneaker shoes
2019-12-03 18:21:26
Sizes from 38 to 44
Comes in 5 different colour
Rated 4.3 out of 5 stars

Worker boots
2018-11-05 10:12:15
Sizes from 38 to 44
Comes in 2 different colour
Rated 3.7 out of 5 stars

Slippers
2018-10-05 13:15:44
Sizes from 38 to 42
Comes in 3 different colour
Rated 4.1 out of 5 stars

Hope to have some answers to my python codes.希望对我的python代码有一些答案。 Am still learning from it as it is part of my project.我仍在从中学习,因为它是我项目的一部分。 I am also restricted to the use of python standard library only, so I can't use any third-party libraries.我也仅限于使用python标准库,所以我不能使用任何第三方库。

Below (The data under 'Information' is a list and you need to iterate over the list entries)下面(“信息”下的数据是一个列表,您需要遍历列表条目)

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')
with open("shoes_db.txt", 'w') as f:
    for entry in data:
        f.write(entry['Name'] + '\n')
        f.write(entry['Date & Time'] + '\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM