【问题标题】:How do I save output into a text file in python?如何将输出保存到python中的文本文件中?
【发布时间】:2016-11-12 01:45:07
【问题描述】:

所以我想做的就是将这个程序的输出保存到一个文本文件中。

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

我正在运行 python 2.7

【问题讨论】:

    标签: python text output


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 2014-09-21
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多