Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

UTF8 Encoding and decoding in python

I have a UTF8 String piped from Java to python.

The end result is

'\xe0\xb8\x9a\xe0\xb8\x99'

Hence for example

a = '\xe0\xb8\x9a\xe0\xb8\x99'
a.decode('utf-8') 

gives me the result

u'\u0e1a\u0e19'

however, what i am curious is since the bytes is piped in as UTF-8, why would be

'\xe0\xb8\x9a\xe0\xb8\x99'

instead of u'\u0e1a\u0e19'.

If i were to encode (u'\u0e1a\u0e19') i would get back '\xe0\xb8\x9a\xe0\xb8\x99'.

So what is the inherent difference between these two and how i do actually understand when to use decode and encode.

Answer*

Draft saved
Draft discarded
Cancel
3
  • 2
    ..and even "two hundred twenty-four" is a decimal representation of the binary representation 11100000, which is just a binary representation of some electrons being pushed through some doped silicon, which is just a standard model representation of our somewhat-tenuous understanding of subatomic particles, which something something string theory. Commented Mar 19, 2015 at 1:30
  • @Malvolio So when do i use decode and encode? Say i was to write this string to a file. Do i have to encode u'\u0e1a\u0e19 as UTF8 or would writing \xe0\xb8\x9a\xe0\xb8\x99 to a file show me the corresponding UTF8 character บน in the file Commented Mar 19, 2015 at 1:30
  • @aceminer I expanded my answer significantly to answer your question. Commented Mar 19, 2015 at 1:38

lang-py

AltStyle によって変換されたページ (->オリジナル) /