简体   繁体   中英

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". This will allow you to append the rest of the text.

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

Have Fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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