简体   繁体   English

使用Python打印到文本文件

[英]Printing to text file using Python

I have written the following code and have executed it several times: 我编写了以下代码,并执行了几次:

import statements....

#Scrape some information....
text_file = open("Output.txt", "w")
text_file.write('some text')
for each in array:
   #some code to find the value of row...
   text_file.write(str(row.encode('ascii', errors='ignore')))
   print (row.encode('ascii', errors='ignore'))
text_file.close()

However, only the 'some text' is written to the file. 但是,仅将“某些文本”写入文件。 I have printed the row text and it produces the correct output. 我已经打印了行文本,它会产生正确的输出。 What am I doing wrong? 我究竟做错了什么?

Open the file in as open("Output.txt", "a") instead of just "w". 以open(“ Output.txt”,“ a”)而不是仅以“ w”形式打开文件。 This will allow you to append the rest of the text. 这将允许您附加其余的文本。

https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

Have Fun! 玩得开心!

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

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