繁体   English   中英

Python使用Regex替换单词列表

[英]Python Using Regex to replace a list of words

我想使用RegEx模块替换单词列表,但是即使经过多次尝试,我仍然失败了。

#full_list.txt
#Tab is the delimiter
#The left column is the list of words to be searched
#The right column is the list of words to be replaced

%!あ [×啞]    @#あ [啞]
%!あい きょう [\(愛嬌\)・\(愛▷敬\)]   @#あいきょう [愛嬌]
    .
    .
    .

我的代码如下:

import re

with open('full_list.txt', 'r', encoding='utf-8') as f:
    search_list = [line.strip().split('\t')[0] for line in f]

with open('full_list.txt', 'r', encoding='utf-8') as f:
    replace_list = [line.strip().split('\t')[1] for line in f]

with open('document.txt', 'r', encoding='utf-8') as f:
    content = f.read()

def replace_func(x, content):
    content = re.sub(search_list[x], replace_list[x], content)
    return content

x = 0
while x < 30:
    content = replace_func(x, content)
    x+=1

with open('new_document.txt', 'w', encoding='utf-8') as f:
    f.write(content)

运行代码后,可以替换某些单词,而有些则不能。 这些代码可能出了什么问题?

如果只想替换单词,请不要使用正则表达式,而要使用字符串的replace-Method:

with open('full_list.txt', 'r', encoding='utf-8') as f:
    search_and_replace = [line.strip().split('\t') for line in f]

with open('document.txt', 'r', encoding='utf-8') as f:
    content = f.read()

for search, repl in search_and_replace:
    content = content.replace(search, repl)

with open('new_document.txt', 'w', encoding='utf-8') as f:
    f.write(content)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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