如何将输出保存到python中的文本文件中? [英] How do I save output into a text file in python?

查看:853
本文介绍了如何将输出保存到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)

我正在运行python 2.7

Im running python 2.7

推荐答案

您可以使用open,然后使用write方法作为结果文件处理程序.

You can use open and then write method of the resulting file handler.

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)

这篇关于如何将输出保存到python中的文本文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆