Printing UTF-8-encoded byte string

前端 未结 2 1364
日久生厌
日久生厌 2021-01-20 00:52

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

2条回答
  •  迷失自我
    2021-01-20 01:36

    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')
    ń
    >>> 
    

提交回复
热议问题