如何将输出保存到Python中的文本文件中?
问题描述:
所以我想要做的是将这个程序的输出保存到一个文本文件。如何将输出保存到Python中的文本文件中?
import itertools
res = itertools.product('qwertyuiopasdfghjklzxcvbnm', repeat=3)
for i in res:
print ''.join(i)
进出口运行的Python 2.7
答
您可以使用open
,然后将生成的文件处理程序的write
方法。
import itertools
res = itertools.product('qwertyuiopasdfghjklzxcvbnm', repeat=3)
with open('output.txt', 'w') as f:
for group in res:
word = ''.join(group)
f.write(word+'\n')
print(word)
感谢它的工作。 –