我使用此代码将非结构化文本文件拆分为其标记并将每个标记输出在一行中:
with open("C:\\...\\...\\...\\record-13.txt") as f:
lines = f.readlines()
for line in lines:
words = line.split()
for word in words:
print (word)
现在我想将输出保存到一个新的文本文件而不是打印它,我将代码修改为:
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()
但它不起作用。你能告诉我这有什么问题吗?