Convert a list with wrong encoding to utf8

Chris Angelico rosuav at gmail.com
Thu Feb 14 11:44:47 EST 2019


On Fri, Feb 15, 2019 at 3:41 AM <vergos.nikolas at gmail.com> wrote:
>
> Hello, i have tried the following to chnage encoding to utf8 because for some reason it has changed regarding list names
>
> [python]
>         #populate client listing into list
>         names.append( name )
>     ....
>     ....
>         names.append( '' )
>         names.sort()
>
>         for name in names:
>                 name = name.encode('latin1').decode('utf8')
> [/python]
>
> and the error that was presented was:
>
> [output]
> UnicodeEncodeError('latin-1', 'Άκης Τσιάμης', 0, 4, 'ordinal not in range(256)')
> [/output]
>
> Why it cannot encode in latin nad decode in utf8 normally?
> And since 'names' are being fetced from mysql database, which they were stored as utf8 strings WHY/HOW the 'names' enrolled in latin-1?

You're going to have to figure out what encoding they are ACTUALLY in,
or (since it looks like you're working with strings) what encoding
they were decoded using. Without that information, all you're doing is
taking stabs in the dark.

BTW, you should probably fix your encodings *before* attempting to
sort the names. It doesn't make much sense to sort by byte values in
mojibake.

ChrisA



More information about the Python-list mailing list