第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python-寫入txt文件作為我的字典列表

Python-寫入txt文件作為我的字典列表

嗶嗶one 2023-06-27 17:22:52
假設(shè)我有這樣存儲的字典數(shù)據(jù)集列表: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']}]我一直試圖將數(shù)據(jù)寫入txt文件,但我不知道如何將其編碼出來。這就是我所做的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()因為“名稱”和“日期和時間”中的值已正確存儲到文件中。但是,對于“信息”,我無法將每個鞋子信息存儲到新行中,并且沒有方括號和“”,因為它是一個列表。這是我想在 Shoes_db.txt 文件中寫入和存儲的內(nèi)容Sneaker shoes2019-12-03 18:21:26Sizes from 38 to 44Comes in 5 different colourRated 4.3 out of 5 starsWorker boots2018-11-05 10:12:15Sizes from 38 to 44Comes in 2 different colourRated 3.7 out of 5 starsSlippers2018-10-05 13:15:44Sizes from 38 to 42Comes in 3 different colourRated 4.1 out of 5 stars希望我的 python 代碼能得到一些答案。我仍在從中學(xué)習(xí),因為它是我項目的一部分。我也被限制只能使用python標(biāo)準(zhǔn)庫,所以我不能使用任何第三方庫。
查看完整描述

2 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗 獲得超2個贊

下面(“信息”下的數(shù)據(jù)是一個列表,您需要迭代列表條目)


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')


查看完整回答
反對 回復(fù) 2023-06-27
?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗 獲得超4個贊

with open("shoes_db.txt", 'w') as f:

    for entry in data:

        f.write(entry['Name'] + '\n')

        f.write(entry['Date & Time'] + '\n')


查看完整回答
反對 回復(fù) 2023-06-27
  • 2 回答
  • 0 關(guān)注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號