Printing UTF-8-encoded byte string

情到浓时终转凉″ 提交于 2019-12-20 01:35:28

问题


I have a data of a form: v = "\xc5\x84"

This is a byte representation of an utf-8 encoded character "ń".

How can I print >>ń<< using variable v?

I'm using python 2.7.2

In original the variable v contained string:

v = "\\xc5\\x84" (double backslashes)

vs

v = "\xc5\x84" (single backslashes)

which is by itself valid utf-8 character.


回答1:


Edit In my machine the output depends on the shell/python used, as shown below.
As commented by Klaus a major actor here would be the locale setting in your system.

>>> v = "\xc5\x84"

>>> print v   #in pycrust shell python 2.6
Å„
>>>

>>> print (v) #in idle python 3.2
Å
>>> 

the machine has the following settings:

>>> import locale
>>> locale.getlocale()
('es_ES', 'cp1252')

Independently of this setting, you get your character with

>>> print v.decode('utf-8')
ń
>>> 



回答2:


Uhm, you don't need to do anything special... It's just print v?

>>> v = "\xc5\x84"
>>> print v
ń


来源:https://stackoverflow.com/questions/8873517/printing-utf-8-encoded-byte-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!