![](/img/trans.png)
[英]How do I take info from a text file and put it into a dictionary (Python)
[英]How do I write a dictionary info a text file in this format? (Python)
假設我有這樣的字典:
{itemone : 13, itemtwo : 243, itemthree : 47}
我如何將它變成一個文本文件,所以它是這種格式:
itemone 13
itemtwo 243
itemthree 47
您可以使用with語句打開要寫入的文件並傳入w+
作為參數:
d = {'itemone': 13, 'itemtwo': 243, 'itemthree': 47}
with open('file.txt', 'w+') as file_handle:
for key, val in d.items():
file_handle.write(f'{key} {val}\n')
文件.txt :
itemone 13
itemtwo 243
itemthree 47
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.