首页 > 解决方案 > 替换文件中的单词

问题描述

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 文件中的“不适当”词。

标签: pythonfilefile-handling

解决方案


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语句。


推荐阅读