如何在python中将输出保存到文本文件中?

2024-03-28 16:31:22 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我要做的是把这个程序的输出保存到一个文本文件中。在

import itertools
res = itertools.product('qwertyuiopasdfghjklzxcvbnm', repeat=3)
for i in res: 
print ''.join(i)

我正在运行python2.7


Tags: inimport程序forresproductrepeatprint
1条回答
网友
1楼 · 发布于 2024-03-28 16:31:22

您可以使用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)

相关问题 更多 >