[英]remove a word from string if it exists in txt file in python
Hey so I would like to remove a word from my string if this specific word is in my text file.嘿,如果这个特定的词在我的文本文件中,我想从我的字符串中删除一个词。 is this possible without storing my whole file in a list如果不将我的整个文件存储在列表中,这可能吗
removable_words # in this case is a txt file
text = [w for w in text.split() if w not in removable_words]
I recommend you using string.replace(target,replacement) for this.我建议您为此使用 string.replace(target,replacement)。
First you need to read contents of.txt file that you have( like this ).首先,您需要阅读您拥有的 .txt 文件的内容( 像这样)。 I assume your txt looks something like this:我假设您的 txt 看起来像这样:
Word1,Word2,Word3,Word4字 1,字 2,字 3,字 4
Once you have converted.txt file content into an array you can do something like this:将.txt 文件内容转换为数组后,您可以执行以下操作:
for word in removable_words:
text.replace(word,"") // This will replace word with empty string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.