我是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.)