如何在Python 2中将字符串更改为Unicode? [英] How to change a string to Unicode in Python 2?

查看:131
本文介绍了如何在Python 2中将字符串更改为Unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如 s1 =\xed\xf3\xb4\x90

>>> x = u"\xed\xf3\xb4\x90"
>>> print x
íó´

如何使用 s1 打印这个?

我尝试过:

s1= "\xed\xf3\xb4\x90"
print unicode(s1)

但我无法获得íó'。如何获得íó'

But I could not get íó´. How could I get íó´?

推荐答案

正确的编解码器这里使用的是'latin1'

The correct codec to be used here is 'latin1':

>>> s1= "\xed\xf3\xb4\x90"
>>> print s1.decode('latin1')  # same as: unicode(s1, 'latin1')
íó´






但是使用'unicode-escape'也可以在这里作为'unicode-转义'假定字节编码在'latin1'中,OP的字符串中没有unicode转义:


However using 'unicode-escape' also works here as 'unicode-escape' assumes the bytes are encoded in 'latin1' and there are no unicode escapes in the OP's string:

>>> s1= "\xed\xf3\xb4\x90"
>>> print s1.decode('unicode-escape')  # same as: unicode(s1, 'unicode-escape')
íó´

这篇关于如何在Python 2中将字符串更改为Unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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