小编Fra*_*ank的帖子

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

我是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'
Run Code Online (Sandbox Code Playgroud)

我期待最后一个字符串u'ueber'.

我最终想做的是用'ue'替换文件中的所有u-umlaut:

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

谢谢你的帮助!(我使用的是Python 2.3.)

python string replace utf-8

5
推荐指数
4
解决办法
3万
查看次数

标签 统计

python ×1

replace ×1

string ×1

utf-8 ×1