![](/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.