UnicodeEncodeError:'ascii'编解码器不能编码字符u'\xe7'在位置17710:序数不在范围(128) [英] UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128)

查看:1553
本文介绍了UnicodeEncodeError:'ascii'编解码器不能编码字符u'\xe7'在位置17710:序数不在范围(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从存档的网页抓取中打印字符串,但是当我这样做时会收到此错误:

I'm trying to print a string from an archived web crawl, but when I do I get this error:

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



当我尝试打印 unicode ['html'])我得到:

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

可以正确地编码这个字符串,或至少得到它打印?感谢。

Any idea how I can properly code this string, or at least get it to print? Thanks.

推荐答案

您需要编码所保存的unicode才能显示,解码 it - unicode是未编码的形式。您应该始终指定一个编码,以便您的代码是可移植的。 通常选择是 utf-8

You need to encode the unicode you saved to display it, not decode it -- unicode is the unencoded form. You should always specify an encoding, so that your code will be portable. The "usual" pick is utf-8:

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



如果不指定编码,无论它是否工作将取决于你打印到你的编辑器,操作系统,终端程序等。

If you don't specify an encoding, whether or not it works will depend on what you're printing to -- your editor, OS, terminal program, etc.

这篇关于UnicodeEncodeError:'ascii'编解码器不能编码字符u'\xe7'在位置17710:序数不在范围(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆