Convert a list with wrong encoding to utf8

vergos.nikolas at gmail.com vergos.nikolas at gmail.com
Thu Feb 14 14:02:14 EST 2019


Τη Πέμπτη, 14 Φεβρουαρίου 2019 - 8:16:40 μ.μ. UTC+2, ο χρήστης Calvin Spealman έγραψε:
> If you see something like this
> 
> '\xce\x86\xce\xba\xce\xb7\xcf\x82
> \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82'
> 
> then you don't have a string, you have raw bytes. You don't "encode" bytes,
> you decode them. If you know this is already encoded as UTF-8 then you just
> need the decode('utf8') part and *not* the encode('latin1') step.
> 
> encode() is something that turns text into bytes
> decode() is something that turns bytes into text
> 
> So, if you already have bytes and you need text, you should only want to be
> doing a decode() and you just need to specific the correct encoding.

I Agree but I don't know in what encoding the string is encoded into. 

I just tried 

names = tuple( [s.decode('utf8') for s in names] )

but i get the error of:

AttributeError("'str' object has no attribute 'decode'",)

but why it says s is a string object? Since we have names in raw bytes is should be a bytes object?

How can i turn names from raw bytes to utf-8 strings? 

ps. Who encoded them in raw bytes anyways? Since they fetced directly from the database shouldn't python3 have them stored in names as utf-8 strings? why raw bytes instead?


More information about the Python-list mailing list