【发布时间】:2018-07-30 11:32:31
【问题描述】:
我正在使用库 unidecode 将重音字符串转换为 ascii 表示的搅拌。
>>> accented_string = u'Málaga'
# accented_string is of type 'unicode'
>>> import unidecode
>>> unidecode.unidecode(accented_string)
>>> Malaga
但问题是我正在从文件中读取字符串,如何将其发送到“unidecode”库。
for name in strings:
print unidecode.unidecode(u+name) #?????
我无法理解它?如果我对其进行编码,那只会给我错误的编码。
【问题讨论】:
-
strings阅读怎么样? -
从 csv 文件到 panda 数据框,然后遍历每个字符串值,每个字符串值的类型都是“字符串”。
-
请在您的问题中也包含该代码。
-
忽略您在示例中看到的“u”;它只是 Python 2 表示法告诉你它是 unicode。如果您的字符串还不是 unicode,您需要知道它们的编码并将它们从
str转换为 unicode。 -
如果这不是大型现有程序的一部分,我强烈建议您立即安装 Python 3 并开始使用它。在 2018 年试图弄清楚 Python 2 的字符编码方法是一种自虐练习。
标签: python python-2.7 unicode utf