简体   繁体   English

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

[英]How do I save output into a text file in python?

So what I want to do is save the output of this program into a text file. 所以我要做的是将该程序的输出保存到文本文件中。

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

Im running python 2.7 我正在运行python 2.7

You can use open and then write method of the resulting file handler. 您可以使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM