paroxyzm
paroxyzm

Reputation: 1499

Printing UTF-8-encoded byte string

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.

Upvotes: 4

Views: 7535

Answers (2)

joaquin
joaquin

Reputation: 85605

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

Upvotes: 7

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

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

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

Upvotes: -2

Related Questions