简体   繁体   English

将python函数输出写入.txt文件

[英]Writing python function outputs to .txt file

So I have these functions that are meant to print/write their outputs to a text file. 因此,我具有这些功能,旨在将其输出打印/写入文本文件。 Now when have these functions print their output in the python shell, I get what I need. 现在,当这些函数在python shell中打印其输出时,我便得到了所需的东西。 It's when I try to have those outputs written to the text file, things go awry. 当我尝试将这些输出写入文本文件时,事情变得不对劲了。

Here are the functions in particular: 具体功能如下:

def printsection1(animals, station1, station2):
    for animal in animals:
        print(animal,'                 ',station1.get(animal, 0),'                   ',station2.get(animal, 0))

def printsection2(animals, station1, station2):
    for animal in animals:
        if station2.get(animal, 0)+station1.get(animal, 0) >= 4:
            print(animal)

def printsection3(animals, station1, station2):
    for animal in animals:
        print(animal,'                    ',int(station1.get(animal, 0))+int(station2.get(animal, 0)))

def printsection4(items):
    import statistics
    most_visits=[]
    for animal, date, station in items:
        most_visits.append(date[0:2]) 

    print(statistics.mode(most_visits))

The code I have in the main() function that writes to the text file looks like : 我在main()函数中写入文本文件的代码如下所示:

outfile.write("Number of times each animal visited each station:\n")
outfile.write("Animal ID           Station 1           Station 2 \n")
printsection1(animals, station1, station2)
outfile.write('\n')
outfile.write("============================================================ \n")

outfile.write("Animals that visited both stations at least 4 times \n")
printsection2(animals, station1, station2)
outfile.write('\n')
outfile.write("============================================================ \n")

outfile.write("Total number of visits for each animal \n")
printsection3(animals, station1, station2)
outfile.write('\n')
outfile.write("============================================================ \n")

outfile.write("Month that has the highest number of visits to the stations \n")
printsection4(items)

Is there a simple way to have the outputs of the functions written to a text file? 有没有简单的方法可以将函数的输出写入文本文件? I've seen the ">>" operator floating around but I can't seem to get it to work. 我曾经看到过“ >>”运算符,但似乎无法正常运行。 If more information is required, I can provide it. 如果需要更多信息,我可以提供。

Thanks again! 再次感谢!

You need to change the calls to print in the functions to outfile.write as well, or use the print(..., file=outfile) syntax. 您还需要将函数中的print调用更改为outfile.write ,或使用print(..., file=outfile)语法。

Alternatively, you can make the standard output point to outfile using an assignment sys.stdout = outfile . 另外,您也可以使标准输出点outfile使用赋值sys.stdout = outfile That will cause all subsequent calls to print to write to outfile . 这将导致所有后续print调用写入outfile

in python 3 you can redirect the output of print function to a file with file keyword : 在python 3中,您可以将print函数的输出重定向到具有file关键字的file

with open('somefile.txt', 'rt') as f:
  print('Hello World!', file=f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM