【发布时间】:2016-09-17 20:46:20
【问题描述】:
我的问题:
我需要将字典中的每个项目输出到 Python 中的外部文本文件。 像下面这样:
dict1 = {}
gen1 = 1
aha = 2
dict1['Generation'] = gen1
dict1['Population'] = aha
for key, value in sorted(dict1.items()):
print(key,':',value, file = open('text.txt', 'w'))
在此示例中,我在字典中有 2 个键,但是当我运行代码并转到文件时,仅输出第一个键的唯一行。
我该怎么做才能将字典中的所有键都打印到外部文件中?
谢谢
【问题讨论】:
标签: python file python-3.x dictionary printing