【问题标题】:Saving output in python [duplicate]在python中保存输出[重复]
【发布时间】:2015-12-28 16:23:49
【问题描述】:

我有这个代码:

import itertools
res = itertools.permutations('abcdefghijklmnopqrstuvwxyz',5) # 5 is the length of the result. 
for i in res: 
   print ''.join(i)

我需要将结果保存在 .txt 文件中,而不是打印 print ''.join(i)

我不熟悉python。感谢您的宝贵时间!

【问题讨论】:

标签: python python-2.7 python-3.x


【解决方案1】:

您可以在写入模式下open 文件,只需使用fileobject.write 方法将您的排列写入文件:

with open('file_name.txt','w') as f:
    res = itertools.permutations('abcdefghijklmnopqrstuvwxyz',5) # 5 is the length of the result. 
    for i in res: 
       f.write(''.join(i)+'\n') 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 2011-10-31
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2019-04-18
    • 2019-03-30
    相关资源
    最近更新 更多