通过使用BeautifulSoup进行get抓取,我得到了一个查询字符串参数,该参数最终表示为:
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'但这让我回到了第一阶段.
从u'\xc3\xa9cosyst\xc3\xa8mes'到u'écosystèmes'怎么走?
发布于 2019-03-24 11:31:07
UTF-8被解码为拉丁语-1,所以解决方案是将其编码为拉丁语-1,然后解码为UTF-8。
>>> s = u'\xc3\xa9cosyst\xc3\xa8mes'
>>> s.encode('latin-1').decode('utf-8')
u'\xe9cosyst\xe8mes'
>>> print s.encode('latin-1').decode('utf-8')
écosystèmes发布于 2019-03-24 11:31:48
我想这会有帮助的:bytes(u'\xc3\xa9cosyst\xc3\xa8mes', 'latin-1').decode('utf-8')
https://stackoverflow.com/questions/55323297
复制相似问题