【问题标题】:Python 3 - print utf-8 encoded data into console (not "\x00(\x00A\x04" )Python 3 - 将 utf-8 编码数据打印到控制台(不是 "\x00(\x00A\x04" )
【发布时间】:2013-12-20 22:18:58
【问题描述】:
r = requests.get('...', allow_redirects=True)
pagetext = r.text
tree = etree.HTML(pagetext)
node = tree.xpath('...')[0]
out = str(etree.tostring(node, method='text', encoding='UTF8'))
print(out) // some "\x00(\x00A\x04>\x042\x04<\x045\x04A\"-like thing is printed

我已经在字符串的不同部分尝试了各种 .encode('UTF-8') 但仍然没有运气:(

【问题讨论】:

  • 检查sys.getdefaultencoding()。你的操作系统是什么?

标签: python-3.x console utf


【解决方案1】:

那不是 UTF-8。

3>> b"\x00(\x00A\x04>\x042\x04<\x045\x04A".decode('utf-16be')
'(Aовмес'

请注意,“utf-16be”是根据您的样本数据选择的;它更有可能是 UTF-16LE。

【讨论】:

    猜你喜欢
    • 2012-07-29
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    相关资源
    最近更新 更多