python - 如何在 Python 中搜索和替换 utf-8 特殊字符?

标签 python string utf-8 replace

我是 Python 初学者,遇到了 utf-8 问题。

我有一个 utf-8 字符串,我想用 ASCII 替换替换所有德语变音符号(在德语中,u-umlaut 'ü' 可以重写为 'ue')。

u-umlaut 的 unicode 代码点为 252,所以我尝试了这个:

>>> str = unichr(252) + 'ber'
>>> print repr(str)
u'\xfcber'
>>> print repr(str).replace(unichr(252), 'ue')
u'\xfcber'

我希望最后一个字符串是 u'ueber'

我最终想做的是用“ue”替换文件中的所有 u-umlauts:

import sys
import codecs      
f = codecs.open(sys.argv[1],encoding='utf-8')
for line in f: 
    print repr(line).replace(unichr(252), 'ue')

感谢您的帮助! (我使用的是 Python 2.3。)

最佳答案

我会定义一个特殊字符的字典(我想映射)然后我使用 translate 方法。

line = 'Ich möchte die Qualität des Produkts überprüfen, bevor ich es kaufe.'

spcial_char_map = {ord('ä'):'ae', ord('ü'):'ue', ord('ö'):'oe', ord('ß'):'ss'}
print(line.translate(spcial_char_map))

你会得到如下结果:

Ich moechte die Qualitaet des Produkts ueberpruefen, bevor ich es kaufe.

关于python - 如何在 Python 中搜索和替换 utf-8 特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2054746/

相关文章:

python - PysimpleGUI 消息框

python - 使用 glob.glob 在目录中的每个文件中搜索并打印一行

python - 根据条件迭代地将一个 df 的行猫到另一个 df 的单元格

unicode - Unicode 和 UTF-8 有什么区别?

php - 如何在 mySQL 数据库表中插入法语字符?

php - 如何将字符串保存在mysql数据库中

python - 如何在Flask中异步调用函数?

python - 清除python中的无效转义?

C++:,当我使用字符串数组时,出现字符串下标超出范围

c# - 将像 JSON 这样的字符串从 C# 发送到 javascript