python - 替换文件中的单词
问题描述
badcontent = []
filebadword = "badwords.txt"
with open(filebadword, 'r') as read_file:
badcontent = read_file.readlines()
goodcontent = []
filegoodword = "goodword.txt"
with open(filegoodword, 'r') as read_file:
goodcontent = read_file.readlines()
msgfile = "msg.txt"
file = open(msgfile, "r")
for word in file:
if word in badcontent:
file = file.write(word.replace([badcontent],[goodconent]))
print(file.readline())
file.close()
elif():
print(file.readline())
file.close()
我想尝试用友好的词替换文本 msg 文件中的“不适当”词。
解决方案
Python 有string.replace(old, new)
- 方法。您现在尝试用列表替换一个单词,这将导致错误。这是一个如何阅读全文的示例:
from random import randint
with open("text_msg_file.txt", 'rb') as f:
lines = f.readlines()
# Text file containing bad words, assume only one word/line
with open("badcontent.txt", 'rb') as f:
badcontent = f.readlines()
# Text file containing good words, assume only one word/line
with open("goodcontent.txt", 'rb') as f:
goodcontent = f.readlines()
# Strip new line character from words
lines = [word.strip("\n") for word in lines]
badcontent = [word.strip("\n") for word in badcontent]
goodcontent = [word.strip("\n") for word in goodcontent]
for i in range(len(lines)):
line = lines[i]
# List of words on single line. Line splitted from whitespaces
words = line.split(" ")
# Loop through all words
for j in range(len(words)):
# Get random integer for index
index = randint(0, len(goodcontent))
if words[j] in badcontent:
# Replace bad word with a good word
words[j] = goodcontent[index]
# Join all words from a list into a string
line = " ".join(words)
# Put string back to list of lines
lines[i] = line
# Join all lines back into one single text
new_text = "\n".join(lines)
with open("new_msg.txt", "wb") as f:
f.write(new_text)
这会将带有替换单词的文本写入文件new_msg.txt
。使用 Python 2.7 使用'rb'
和'wb'
foropen
语句来启用以二进制模式打开,因此代码更加健壮。在 Python 3 中仅使用'r'
和'w'
foropen
语句。
推荐阅读
- android - 当 JSON 数据是动态的时,如何在 Android 中进行改造?
- javascript - 如何在 Linnworks 嵌入式应用程序中获取刷新的令牌?
- sql - VBA SQL字符串将短日期保存为通用日期而不是短日期
- javascript - 使用异步调用在浏览器中开始下载文件
- r - 从这些向量名称的列表中获取向量列表
- aframe - 在 aframe 中向 DPDB 添加条目
- ruby-on-rails - 如何为多态关系编写 :within 范围?
- google-maps - 如何处理新 Places API Billing 中的 Sessions?
- java - PSO 算法中的环形拓扑
- node.js - Sails.js:如何根据关联集合中的值查找记录?