UnicodeEncodeError:“ascii”编解码器无法对位置17710中的字符u“\xe7”进行编码:序号不在范围(128)内

2024-05-14 15:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从archived web crawl中打印字符串,但当我打印时,出现以下错误:

print page['html']
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128)

当我尝试打印unicode(page['html'])时,我得到:

print unicode(page['html'],errors='ignore')
TypeError: decoding Unicode is not supported

知道如何正确地编码这个字符串,或者至少让它打印出来吗?谢谢。


Tags: 字符串inwebhtml错误asciipageunicode
1条回答
网友
1楼 · 发布于 2024-05-14 15:35:58

您需要将保存的unicode编码为显示它,而不是解码为未编码的形式。您应该始终指定一个编码,以便您的代码是可移植的。“通常”的选择是utf-8

print page['html'].encode('utf-8')

如果您不指定编码,它是否工作将取决于您的编辑器、操作系统、终端程序等

相关问题 更多 >

    热门问题