2011-07-12 27 views
3
print "I can write my function to a txt file:" 
my_function = raw_input("File name? ") 

print_bills = input(bills(1010, 200, 100, 120)) 

open(my_function, 'w') 
my_function.write('%r' % (print_bills)) 
my_function.close() 

我在快乐蟒蛇工作,我试图让我的功能写入到一个txt文件,功能是这样的我已经定义了一个函数,并希望将它的输出写入一个txt文件,我需要在代码中更改哪些内容?

def bills(mortgage, elec, oil, ph_int_tv): 

    print "Our upcoming mortgage will be approximately %d /month)" % mortgage 

    print "Split between my brother and I, that will be %d /month" % (mortgage/2) 

    print "With a third roomate, it will be %d /month" % (mortgage/3) 

    print "My total bill, per month, with living costs included will be %d /month" % ((mortgage/2)+(elec/2)+(oil/2)+(ph_int_tv/2)) 

    print "I better start saving money!!\n" 

我很新,刚开始瓦特/ LPTHW在线图书...所以我的问题是,我可以在顶级代码中进行更改以使其工作....如果有的话?

回答

3

您可以重新分配标准输出

imporst sys 
f = open("test.txt", "w") 
sto = sys.stdout # save stdout so we can revert back to it 
sys.stdout = f # from now on, anything that is printed will go to test.txt 

# here, call any method that calls print 
# or even 
print "hello" 

#finally when no more redirection is desired 
f.close() 
sys.stdout = sto # reestablish the regular stdout 

您也可以找到有用的提示在此StackOverflow question一个稍微不同的目的。注意那里的一些解决方案可能只适用于Unix系统。现在

,而上面显示的诀窍是重定向控制台(和/或标准错误,BTW)输出的权宜之计路,而“我能在上面的代码更改为得到这个工作”在暗示这种重定向方法的问题。你可以考虑通过传递一个文件来改写账单(),方法是用sys.stdout或你自己选择的文件调用它。类似:

from __future__ import print_function # might as well get used to Python 3.0 print syntax 

def bills(outFile, mortgage, elec, oil, ph_int_tv): 
    print("Our upcoming mortgage will be approximately %d /month)" % mortgage, file=outFile) 
    print("Split between my brother and I, that will be %d /month" % (mortgage/2), file=outFile) 
    print("With a third roomate, it will be %d /month" % (mortgage/3), file=outFile) 
    print("My total bill, per month, with living costs included will be %d /month" 
      % ((mortgage/2)+(elec/2)+(oil/2)+(ph_int_tv/2)), file=outFile) 

    print("I better start saving money!!\n", file=outFile) 
+0

我想我看到你去这里,但正如我所说,我是新的,仍在学习。因此,如果我导入sys,将txt文件设置为f,并将所有标准输出设置为f,那么运行我的函数将打印到txt文件中? –

+0

@ New_Guy101。究竟!一旦sys.stdout被重新分配,通常会进入屏幕的任何内容都会转到文本文件。请注意,一旦完成,我们将stdout重新分配回原始值。另外还要注意Wim在下面的回应中使用了'with construct'以及异常捕获和finaly子句以确保无论如何,stdout都可以正确地重置为原来的值。值。另外请注意我的观点,虽然这种方法(重新分配stdout)的洞察力和方便性可能不是推荐采伐/管道的推荐方式。 – mjv

+0

绝对,我仍然试图完全掌握'进口系统',虽然我认为我开始得到它...我明白设置标准输出到txt文件的想法,但我并没有完全改变标准输出并将其更改回来。我已经阅读了很多pydoc的东西,并且了解了一些东西,但是我真的试图找到一些将这些东西放入外行的术语,所以我可以开始完全掌握程序员试图说些什么....此外,这工作非常好,所以谢谢你:-) –

0

基本上,在Python,标准输出可以被处理(几乎)像任何其他文件对象。因此,您可以将文件对象分配到sys.stdoutprint语句将写入文件而不是打印在终端窗口上。

像下面这样的东西可能会起作用。

import sys 

myfile = open('myfile.txt', 'w') 
stdout_bckp = sys.stdout 
sys.stdout = myfile 
bills(1010, 200, 100, 120) 
sys.stdout = stdout_bckp 
myfile.close() 

如果你对某种UNIX机器,你也可以从你从终端开始为这样一个程序的输出重定向:

$ python myscript.py > myfile.txt 
0
def bills(mortgage, elec, oil, ph_int_tv): 
    print "Our upcoming mortgage will be approximately %d /month)" % mortgage 
    print "Split between my brother and I, that will be %d /month" % (mortgage/2) 
    print "With a third roomate, it will be %d /month" % (mortgage/3) 
    print "My total bill, per month, with living costs included will be %d /month" % ((mortgage/2)+(elec/2)+(oil/2)+(ph_int_tv/2)) 
    print "I better start saving money!!\n" 

def my_function(my_file,print_bills): 
    f=open(my_file, 'w') 
    f.write('%r' % (print_bills)) 
    f.close() 

print "I can write my function to a txt file:" 
my_file = raw_input("File name? ") 

print_bills = bills(1010, 200, 100, 120) 


my_function(my_file, print_bills) 
+0

感谢兄弟...我试过这个,但不幸的是它没有工作。错误消息给了我一个关于Back_to_the_future函数的python链接......我读了它,但并不完全理解他们在说什么。下次我们冷静下来,你必须给我完整的分解 –

相关问题