2012-09-06 25 views
0

是否可以将输出到python shell的信息保存到txt文件中。将特定信息保存到python shell的txt

我运行Python版本2.7.2

这是什么显示在外壳。

Enter numbers 1... 22,34,35,40,55 
Enter numbers 2... 12,14,34,47,49 
Enter numbers 3... 1,4,10,19,30 
Section 1 
The number seqeuce is # 0 0 1 2 1 1 # <-- 
Section 2       
The number seqeuce is # 0 2 0 1 2 0 # <-- This is what I would like to save to txt. 
Section 3       
The number seqeuce is # 2 2 0 1 0 0 # <-- 

我环顾四周,并了解如何读,写和附加文件,但不 直接从壳。

我将不胜感激任何建议来解决这个问题。

回答

0

它在shell中是完全一样的,但是你将基本上一次运行一行代码,而不是仅仅运行一个文件。否则,你可以复制/粘贴结果... 不太确定,如果这就是你要找的,请告诉我,如果它不是。

+0

写的代码是问这个问题,shell给出了答案。如果可能,我想直接从shell中保存答案。 – Howie

+0

我认为最简单的方法就是复制/粘贴答案,诚实。否则,你可以尝试挖掘[这里](http://docs.python.org/library/stdtypes.html#bltin-file-objects)或[here](http://docs.python.org/tutorial/ inputoutput.html#读取与写入文件)。 –

+0

感谢您的反馈意见。 – Howie

0
>> numbers = input("Enter numbers") 
    >> with open('yourfile.txt', 'w') as f: 
     f.write('The number sequence is {0}'.format(numbers.replace(',', ' ')) 

如果输入为22,34,35,40,55,输出文件将是The number sequence is 22 34 35 40 55

你只需要键入每个命令到外壳。当您运行.py脚本时,解释器只是逐行评估您的代码,就像shell在键入命令时所做的一样。