我正在阅读和解析亚马逊的XML文件,但当XML文件显示一个 ' 时,我尝试打印它时会出现以下错误:
'ascii' codec can't encode character u'\u2019' in position 16: ordinal not in range(128)
根据我目前在网上阅读的内容,错误是由于XML文件采用UTF-8编码,而Python希望将其处理为ASCII编码字符所致。是否有简单的方法消除错误并使我的程序在读取时打印XML呢?
unicodeData.encode('ascii', 'ignore')
>>> # Python 2: u = unichr(40960) + u'abcd' + unichr(1972)
>>> u = chr(40960) + u'abcd' + chr(1972)
>>> u.encode('utf-8')
'\xea\x80\x80abcd\xde\xb4'
>>> u.encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in position 0: ordinal not in range(128)
>>> u.encode('ascii', 'ignore')
'abcd'
>>> u.encode('ascii', 'replace')
'?abcd?'
>>> u.encode('ascii', 'xmlcharrefreplace')
'ꀀabcd޴'
.encode('ascii', 'ignore')
即使操作环境支持非 ASCII 字符(大多数情况下),也会不必要地丢失数据。 - jfs更好的解决方案:
if type(value) == str:
# Ignore errors even if the string is not proper UTF-8 or has
# broken marker bytes.
# Python built-in function unicode() can do this.
value = unicode(value, "utf-8", errors="ignore")
else:
# Assume the value object has proper __unicode__() method
value = unicode(value)
如果您想了解更多关于为什么的内容:
http://docs.plone.org/manage/troubleshooting/unicode.html#id1
u'\u2019'
已经是Unicode格式的。 - jfsassert isinstance(text, unicode) # or str on Python 3
print(text)
PYTHONIOENCODING
环境变量指定字符编码:$ PYTHONIOENCODING=utf-8 python your_script.py >output.utf8
python your_script.py
应该可以直接使用——您的区域设置用于对文本进行编码(在POSIX上检查:LC_ALL
,LC_CTYPE
,LANG
envvars——如有必要,请将LANG
设置为utf-8语言环境)。这是一篇关于IT技术的优秀文章:http://www.carlosble.com/2010/12/understanding-python-and-unicode/
。该文章讲解了Python和Unicode之间的关系,如果您对此感兴趣,可以仔细阅读该文章。# -*- coding: utf-8 -*-
def __if_number_get_string(number):
converted_str = number
if isinstance(number, int) or \
isinstance(number, float):
converted_str = str(number)
return converted_str
def get_unicode(strOrUnicode, encoding='utf-8'):
strOrUnicode = __if_number_get_string(strOrUnicode)
if isinstance(strOrUnicode, unicode):
return strOrUnicode
return unicode(strOrUnicode, encoding, errors='ignore')
def get_string(strOrUnicode, encoding='utf-8'):
strOrUnicode = __if_number_get_string(strOrUnicode)
if isinstance(strOrUnicode, unicode):
return strOrUnicode.encode(encoding)
return strOrUnicode
你可以使用类似以下形式的内容
s.decode('utf-8')
这将把一个UTF-8编码的字节串转换成Python Unicode字符串。但是使用确切的过程取决于你如何加载和解析XML文件,例如,如果你从未直接访问XML字符串,你可能需要使用codecs
模块中的解码器对象。
myStrings = deque([u'Dorf and Svoboda\u2019s text builds on the str... and Computer Engineering\u2019s subdisciplines.'])
正如您所看到的,该字符串已经是UTF-8编码的,但它对内部的'\u2019'感到不满。 - Alex B'...'.encode('utf-8')
。 - sthunidecode
包:
https://pypi.python.org/pypi/Unidecode
解释可以在这里找到:
https://www.tablix.org/~avian/blog/archives/2009/01/unicode_transliteration_in_python/
这比为给定字符串u使用u.encode('ascii', 'ignore')
更好,如果字符精度不是你想要的,但仍然想要具有人类可读性,则可以避免不必要的麻烦。
Wirawan
我写了以下内容来解决烦人的非ASCII引号问题,并强制转换为可用的格式。
unicodeToAsciiMap = {u'\u2019':"'", u'\u2018':"`", }
def unicodeToAscii(inStr):
try:
return str(inStr)
except:
pass
outStr = ""
for i in inStr:
try:
outStr = outStr + str(i)
except:
if unicodeToAsciiMap.has_key(i):
outStr = outStr + unicodeToAsciiMap[i]
else:
try:
print "unicodeToAscii: add to map:", i, repr(i), "(encoded as _)"
except:
print "unicodeToAscii: unknown code (encoded as _)", repr(i)
outStr = outStr + "_"
return outStr
# _*_ coding:utf-8 _*_
Python 3.5, 2018
如果您不知道编码,但Unicode解析器出现问题,您可以在Notepad++
中打开文件,在顶部菜单栏中选择编码->转换为ANSI
。然后您可以像这样编写Python代码
with open('filepath', 'r', encoding='ANSI') as file:
for word in file.read().split():
print(word)
unicode()
函数清理字符串? - Nick Heiner