UTF-8 encoded byte string printing

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

This is a byte representation of the utf-8 "Ε„" encoded character.

How can I print β†’ Ε„ <<using the variable v?

I am using python 2.7.2

The source variable v contains the line:

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

vs

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

which in itself is a valid utf-8 character.

+4
source share
2 answers

Edit In my machine, the output depends on the shell / python used, as shown below.
As Klaus comments, the main actor here will be setting the locale on 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') 

Regardless of this option, you get your character with

 >>> print v.decode('utf-8') Ε„ >>> 
+7
source

Um, you don’t need anything special ... Is it just print v ?

 >>> v = "\xc5\x84" >>> print v Ε„ 
-2
source

Source: https://habr.com/ru/post/1391161/


All Articles