python - 如果它存在于python的txt文件中,则从字符串中删除一个单词
问题描述
嘿,如果这个特定的词在我的文本文件中,我想从我的字符串中删除一个词。如果不将我的整个文件存储在列表中,这可能吗
removable_words # in this case is a txt file
text = [w for w in text.split() if w not in removable_words]
解决方案
我建议您为此使用 string.replace(target,replacement)。
首先,您需要阅读您拥有的 .txt 文件的内容(像这样)。我假设您的 txt 看起来像这样:
字 1,字 2,字 3,字 4
将 .txt 文件内容转换为数组后,您可以执行以下操作:
for word in removable_words:
text.replace(word,"") // This will replace word with empty string
推荐阅读
- http - 是否可以将 HTTP 用于 Bing Ads Report API?
- javascript - 如何使用 mongoose 使用 API 删除数组中的某些内容?
- node.js - 使用 PassportJs 的 Express 服务器在部署后未将用户数据发送到客户端
- c# - 尝试从 Graph 检索上次登录日期时间时过滤器子句无效
- regex - 删除重复项,保留最后一次出现
- reactjs - 在 textarea Reactjs 中键入时突出显示文本
- excel-formula - 计算单元格上方的非空白单元格,直到第一个空白
- python-3.x - 如何解释 python cProfile 输出
- html - Href 插入列
- java - DateTimeFormatter.parse 返回无效的日语日期