translit.py
應該是這樣的:
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
import fileinput
table = {
0xe4: u'ae',
ord(u'ö'): u'oe',
ord(u'ü'): u'ue',
ord(u'ß'): None,
}
for line in fileinput.input():
s = line.decode('utf8')
print s.translate(table),
你可以使用這樣的:
$ cat utf8.txt
sömé täßt
sömé täßt
sömé täßt
$ ./translit.py utf8.txt
soemé taet
soemé taet
soemé taet
如果你正在使用Python 3的字符串是默認Unicode和你不」需要對它進行編碼如果它包含非ASCII字符或甚至非拉丁字符。因此,該解決方案將看起來如下:
line = 'Verhältnismäßigkeit, Möglichkeit'
table = {
ord('ä'): 'ae',
ord('ö'): 'oe',
ord('ü'): 'ue',
ord('ß'): 'ss',
}
line.translate(table)
>>> 'Verhaeltnismaessigkeit, Moeglichkeit'
來源
2010-04-23 19:23:45
hop
搜索,以便爲 「音譯」,找到相關問題。 – hop 2010-04-23 18:30:50
http://stackoverflow.com/questions/816285/where-is-pythons-best-ascii-for-this-unicode-database/816319#816319 – hop 2010-04-23 19:30:09
給定的Perl腳本實際上只會替換每行上的第一個匹配項,但那肯定是一個意外。 – tripleee 2013-12-15 16:52:08