Print output to text file (.txt) using Python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Print output to text file (.txt) using Python



I want print my output to text file. But the results different if I print in terminal. My code :


...
words = keywords.split("makan","Rina")
sentences = text.split(".")
for itemIndex in range(len(sentences)):
for word in words:
if word in sentences[itemIndex]:
print('"' + sentences[itemIndex] + '."')
break



The ouput like this :


"Semalam saya makan nasi padang."
" Saya makan bersama Rina."
" Rina pesan ayam goreng."



If I add print to text file :


words = ["makan","Rina"]
sentences = text.split(".")
for itemIndex in range(len(sentences)):
for word in words:
if word in sentences[itemIndex]:
with open("corpus.txt",'w+') as f:
f.write(sentences[itemIndex])
f.close()



The output just :


Rina pesan ayam goreng



Why? How to print outputs to text file same like I print outputs in terminal?




3 Answers
3



You are reopening the file for each iteration of the loop so when you write to it you overwrite what is already there. You need to open the file outside of all the loops and open it in append mode, denoted by a.


a



When you finish you will end up with only the last line in the file. Remember to close the file using f.close() when you are done with it.


f.close()





recommending a is bad advice. The problem is NOT that he needs to append on each iteration...he should really just open it once, write what he wants and then close it. Your suggestion of f.close is redundant. When you open a file using the with syntax, it gets closed automatically when the interpreter exits that context.
– Nizebulous
42 mins ago


a


f.close


with





The way that his loop structure is set up, lends itself to using append mode. If he wanted to write it to the file all in one shot, he's going to have to join the list, concatenating in 'n' characters and then write to the file; or something similar.
– John Stark
21 mins ago





It's still better advice than using append mode. But he actually doesn't need to do that...he can call f.write on each iteration as he is doing.
– Nizebulous
20 mins ago



Because you are listing with open inside of the loop, and you're using 'w+' mode, your program is going to overwrite the file each time, so you will only end up with the last line written to the file. Try it with 'a' instead, or move with open outside of the loop.


with open


'w+'


'a'


with open





He needs to move the with open statement out of the loop...the close line is redundant.
– Nizebulous
48 mins ago


with open


close


close


with


with



Your code should be:


words = ["makan","Rina"]
sentences = text.split(".")
with open("corpus.txt",'w+') as f:
for itemIndex in range(len(sentences)):
for word in words:
if word in sentences[itemIndex]:
f.write(sentences[itemIndex] + 'n')






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results