2017-02-13 140 views
0

Python 3中有什麼方法可以替代英文字母的一般語言特定字符嗎?
例如,我有功能get_city(IP),返回與給定的IP連接的城市名稱。它連接到外部數據庫,所以我不能改變它的編碼方式,我只是從數據庫中獲得價值。
我想這樣做:用英文字母替換python中的語言特定字符

city = "České Budějovice" 
city = clear_name 
print(city) #should return "Ceske Budejoice" 

在這裏,我用捷克語,但一般應該在任何非亞洲的langauge工作。

回答

2

嘗試unidecode

# coding=utf-8 
from unidecode import unidecode 

city = "České Budějovice" 
print(unidecode(city.decode('utf-8'))) 

打印Ceske Budejovice根據需要(假設您的文章有一個錯字)。

1

在這種情況下使用unicodedata模塊。
爲了獲得所需的結果,你應該使用unicodedata.normalize()unicodedata.combining()功能正常化給定的字符串:

import unicodedata 

city = "České Budějovice" 
normalized = unicodedata.normalize('NFD', city) 
new_city = u"".join([c for c in normalized if not unicodedata.combining(c)]) 

print(new_city) # Ceske Budejovice 

NFD是四Unicode規範化形式之一

http://www.unicode.org/reports/tr15/