我在处理从不同网页(在不同的网站上)获取的文本中的unicode字符时遇到了问题。我用的是BeautifulSoup。

问题是,误差并不总是可再现的;它有时会处理某些页面,有时会通过抛出UnicodeEncodeError而退出。我几乎尝试了我能想到的所有方法,但我没有找到任何能在不引发某种Unicode相关错误的情况下始终工作的方法。

导致问题的代码部分如下所示:

agent_telno = agent.find('div', 'agent_contact_number')
agent_telno = '' if agent_telno is None else agent_telno.contents[0]
p.agent_info = str(agent_contact + ' ' + agent_telno).strip()

下面是运行上面的代码段时在某些字符串上生成的堆栈跟踪:

Traceback (most recent call last):
  File "foobar.py", line 792, in <module>
    p.agent_info = str(agent_contact + ' ' + agent_telno).strip()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)

我怀疑这是因为某些页面(或者更具体地说,来自某些网站的页面)可能是编码的,而其他页面可能是未编码的。所有的网站都设在英国,并提供英国消费的数据,因此不存在与内化或处理非英语文本相关的问题。

有没有人对如何解决这个问题有什么想法,以便我能够始终如一地解决这个问题?


当前回答

外壳内:

通过以下命令查找支持的UTF-8语言环境:区域设置-a |grep“UTF-8”在运行脚本之前导出它,例如:导出LC_ALL=$(语言环境-a|grep UTF-8)或手动类似:导出LC_ALL=C.UTF-8通过打印特殊字符进行测试,例如。™:python-c'打印(u“\u2122”);'

以上是在Ubuntu中测试的。

其他回答

唉,这至少在Python 3中有效。。。

Python 3

有时错误在环境变量中,因此

import os
import locale
os.environ["PYTHONIOENCODING"] = "utf-8"
myLocale=locale.setlocale(category=locale.LC_ALL, locale="en_GB.UTF-8")
... 
print(myText.encode('utf-8', errors='ignore'))

其中在编码中忽略错误。

我们在Django中使用本地化的fixture运行manage.py迁移时遇到了这个错误。

我们的源代码包含#-*-coding:utf-8-*-声明,MySQL为utf8正确配置,Ubuntu具有适当的语言包和/etc/default/locate中的值。

问题很简单,Django容器(我们使用docker)缺少LANG env变量。

将LANG设置为en_US.UTF-8并在重新运行迁移之前重新启动容器解决了问题。

在运行脚本之前,可以将字符编码设置为UTF-8:

export LC_CTYPE="en_US.UTF-8"

这通常可以解决问题。

这里有简单的助手函数。

def safe_unicode(obj, *args):
    """ return the unicode representation of obj """
    try:
        return unicode(obj, *args)
    except UnicodeDecodeError:
        # obj is byte string
        ascii_text = str(obj).encode('string_escape')
        return unicode(ascii_text)

def safe_str(obj):
    """ return the byte string representation of obj """
    try:
        return str(obj)
    except UnicodeEncodeError:
        # obj is unicode
        return unicode(obj).encode('unicode_escape')

只需添加到变量编码('utf-8')

agent_contact.encode('utf-8')