简体   繁体   中英

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.

First you need to read contents of.txt file that you have( like this ). I assume your txt looks something like this:

Word1,Word2,Word3,Word4

Once you have converted.txt file content into an array you can do something like this:

for word in removable_words:
  text.replace(word,"") // This will replace word with empty string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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