字符串编码和解码? [英] string encoding and decoding?

查看:63
本文介绍了字符串编码和解码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试使用的错误消息.我做错了什么?

string.decode("ascii", "ignore")

<块引用>

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in位置 37:序号不在范围内(128)

string.encode('utf-8', "ignore")

<块引用>

UnicodeDecodeError: 'ascii' 编解码器无法解码字节 0xc2 的位置37:序号不在范围内(128)

解决方案

您无法解码 unicode,也无法编码 str.试试反过来.

Here are my attempts with error messages. What am I doing wrong?

string.decode("ascii", "ignore")

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 37: ordinal not in range(128)

string.encode('utf-8', "ignore")

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 37: ordinal not in range(128)

解决方案

You can't decode a unicode, and you can't encode a str. Try doing it the other way around.

这篇关于字符串编码和解码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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