【发布时间】:2019-03-24 11:26:59
【问题描述】:
通过使用 BeautifulSoup 进行网络抓取,我得到了一个查询字符串参数,该参数最终表示为:
param_value = u'\xc3\xa9cosyst\xc3\xa8mes'
阅读的时候,我猜应该是écosytèmes
我尝试了几种编码/转义/解码的方法(如here 和here 所述)
但我不断收到以下错误:
UnicodeEncodeError('ascii', u'\xc3\xa9cosyst\xc3\xa8mes', 0, 2, 'ordinal not in range(128)')
我也尝试了重复提出的解决方案:
Python 2.7.15 (default, Jul 23 2018, 21:27:06)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'\xc3\xa9cosyst\xc3\xa8mes'
>>> s.encode('latin-1').decode('utf-8')
u'\xe9cosyst\xe8mes'
但它让我回到了第 1 格...
我怎样才能从u'\xc3\xa9cosyst\xc3\xa8mes' 到u'écosystèmes'?
【问题讨论】:
-
相关:Fixing mojibakes in UTF-8 text。你所拥有的看起来像 UTF-8 解码为 latin-1。
-
u'\xe9cosyst\xe8mes'是正确的 unicode 字符串值。您现在应该阅读Understanding repr( ) function in Python
标签: python python-2.7 encoding escaping