我一直在研究将文本转换成ASCII码的方法。因此,ā会变成a,ñ会变成n等等。
unidecode
对此非常有帮助。
# -*- coding: utf-8 -*-
from unidecode import unidecode
print(unidecode(u"ā, ī, ū, ś, ñ"))
print(unidecode(u"Estado de São Paulo"))
生成:
a, i, u, s, n
Estado de Sao Paulo
然而,我无法使用输入文件中的数据复制此结果。
test.txt 文件的内容:
ā, ī, ū, ś, ñ
Estado de São Paulo
# -*- coding: utf-8 -*-
from unidecode import unidecode
with open("test.txt", 'r') as inf:
for line in inf:
print unidecode(line.strip())
产生:
A, A<<, A<<, A, A+-
Estado de SAPSo Paulo
并且:
运行时警告:参数不是Unicode对象。 传递编码字符串可能会产生意外的结果。
问题:我如何将这些行读取为Unicode,以便我可以将它们传递给unidecode
?