简体   繁体   English

如何将输出保存到新的txt文件中?

[英]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? 您能告诉我这是怎么回事吗?

You are opening the file for each token, and because you are opening with mode 'w' the file is truncated. 您正在为每个令牌打开文件,并且由于以模式'w'打开,因此文件被截断了。 You can open with mode 'a' to append to the file, but that would be very inefficient. 您可以使用模式'a'打开以将其追加到文件中,但是这样做效率很低。

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. 我怀疑您希望每个单词都在不同的行上,因此请确保您还要写一个新的行字符。

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

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