【问题标题】:I've defined a function and want to write the output of it to a txt file, what do I need to change in my code?我已经定义了一个函数,并且想将它的输出写入一个 txt 文件,我需要在我的代码中进行哪些更改?
【发布时间】:2011-10-03 06:43:21
【问题描述】:
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()

我正在使用 python 并试图将我的函数写入 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 在线书籍...所以我的问题是,我可以在顶部代码中更改什么以使其正常工作...如果有的话?

【问题讨论】:

    标签: python string function


    【解决方案1】:

    您可以重新分配标准输出

    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 系统。

    现在,虽然上面显示的技巧是重定向控制台(和/或标准错误,顺便说一句)输出的 权宜之计 方式,而 “我可以在顶部代码中更改什么让它工作” 在暗示这种重定向方法的问题中。您可以考虑重写 bills() 方法,向它传递一个文件,使用 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)
    

    【讨论】:

    • 我想我知道你要去哪里了,但正如我所说,我是新手,还在学习。那么如果我导入sys,将txt文件设置为f,并将所有标准输出设置为f,那么运行我的函数会打印到txt文件吗?
    • @New_Guy101。确切地!重新分配 sys.stdout 后,通常会进入屏幕的任何内容都会进入文本文件。请注意,一旦我们完成,我们如何将 stdout 重新分配回其原始值。另请注意 Wim 在下面的响应,其中使用了 with construct 以及异常捕获和 finaly 子句,以确保无论如何,stdout 都会正确重置为其原点。价值。还要注意我的评论,虽然这种方法(重新分配标准输出)有见地和方便,但可能不是引入日志记录/管道的推荐方法。
    • 绝对,我仍在尝试完全掌握“import sys”,尽管我想我已经开始明白了……我理解将标准输出设置为 txt 文件的想法,但是我不太了解更改标准输出并将其更改回来。我已经阅读了很多 pydoc 的东西并理解了一些东西,但我真的想找到一些东西,把很多这些东西放在外行的术语中,所以我可以开始完全掌握程序员想要说的东西......另外,这很有效,所以也谢谢你:-)
    【解决方案2】:
    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)
    

    【讨论】:

    • 谢谢兄弟...我试过了,但不幸的是它没有用。错误消息给了我一个关于 Back_to_the_future 函数的 python 链接......我读了它,但不太明白他们在说什么。下次我们放松时,你必须给我完整的细分
    【解决方案3】:

    基本上,在 Python 中,可以(几乎)像对待任何其他文件对象一样对待标准输出。所以你可以给sys.stdout分配一个文件对象,print语句将被写入文件而不是打印在终端窗口上。

    类似下面的方法可能会起作用。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 2015-12-24
      • 2019-09-12
      相关资源
      最近更新 更多