简体   繁体   English

如何查找和替换Python中的Unicode字符串?

[英]How To Find and Replace Unicode String in Python?

I have a text file containing a list of Unicode strings.我有一个包含 Unicode 个字符串列表的文本文件。 Let's say list.txt and I have another dictionary file dict.txt that contains a list of words (Unicode also), which needs to be searched in the first file and replaced with something else.比方说list.txt ,我有另一个字典文件dict.txt ,它包含一个单词列表(也是 Unicode),需要在第一个文件中搜索并替换为其他内容。 However, my code is executing without error but not doing the find/replace properly.但是,我的代码执行时没有错误,但没有正确执行查找/替换。

list.txt

राम गोपाल
राम प्रसाद

etc.等等

dict.txt

गोपाल
प्रसाद

find_replace.py

import string

# read the dictionary file of terms (each term in one line)
terms = open('dict.txt', encoding='utf-8').read().splitlines()

# read the file that contains terms to be replaced
original = open('list.txt', encoding='utf-8').read()

# initialize
replaced = ""

for term in terms:
    replaced = original.replace(term, u"")

print(replaced)

Any suggestion on how to do this?关于如何做到这一点的任何建议?

you can do this:你可以这样做:

# read the dictionary file of terms (each term in one line)
terms = open('dict.txt', encoding='utf-8').read().splitlines()

# read the file that contains terms to be replaced
original = open('list.txt', encoding='utf-8').read()

# initialize
replaced = original

for term in terms:
    replaced = replaced.replace(term, u'something else')

print(replaced)

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

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