【发布时间】:2021-03-04 15:46:38
【问题描述】:
我正在尝试删除另一个文本文件中的一些单词(位于文本文件中)。尽管我的代码似乎可以工作,但我注意到它在文本文件中的某个点停止删除单词。然后我检查了文本文件,注意到 Python 将所有单词都写在同一行上,并且这一行有字符限制,导致进程停止。我该如何规避呢?
这是我的代码:
# text file list to array
with open('function_words.txt') as functionFile:
functionWords = [word for line in functionFile for word in line.split()]
# delete the word on the text file if it matches one of the array
with open("results.txt", "r+") as newfile:
newfile.write(' '.join(i for i in toPrint.read().split() if i not in functionWords))
提前致谢,如果您需要更多详细信息,请告诉我。
【问题讨论】:
-
加入
\n而不是空格? -
@Sayse 将每个单词都放在一个新行上,这不是我想要做的 :(
-
不清楚你要做什么,尝试用理解来做这件事可能会让你比它需要的更难
标签: python arrays python-3.x python-requests