MACEE
MACEE

Reputation: 2023

How to save the output into a new txt file?

I use this code to split an unstructured text file to its tokens and output each token in one line:

with open("C:\\...\\...\\...\\record-13.txt") as f:
   lines = f.readlines()
   for line in lines:
       words = line.split()
       for word in words:
           print (word)

Now I want to save the output into a new text file instead of printing it, I modify the code to this:

with open("C:\\...\\...\\...\\record-13.txt") as f:
   lines = f.readlines()
   for line in lines:
       words = line.split()
       for word in words:
           file = open ("tokens.txt", "w")
           file.write (word)
           file.close()

but it doesn't work. Would you please tell me what's wrong with that?

Upvotes: 0

Views: 3380

Answers (1)

Tim
Tim

Reputation: 12174

You are opening the file for each token, and because you are opening with mode 'w' the file is truncated. You can open with mode 'a' to append to the file, but that would be very inefficient.

A better way is to open the output file at the very start and let the context manager close it for you. There's also no need to read the entire file into memory at the start.

with open("in.txt") as in_file, open("tokens.txt", "w") as out_file:
   for line in in_file:
       words = line.split()
       for word in words:
           out_file.write(word)
           out_file.write("\n")

I suspect you want each word to be on a different line, so make sure you also write a new line character.

Upvotes: 3

Related Questions