Convert a list with wrong encoding to utf8

Calvin Spealman cspealma at redhat.com
Thu Feb 14 11:44:59 EST 2019


You can only decode FROM the same encoding you've encoded TO. Any decoding
must know the input it receives follows the rules of its encoding scheme.
latin1 is not utf8.

However, in your case, you aren't seeing problem with  the decoding. That
step is never reached. It is failing to encode the string as latin1 because
it is not compatible with the latin1 scheme. Your string contains
characters which cannot be represented in latin1.

It really is not clear what you're trying to accomplish here. The string
encoding was already handled when you pulled this out of the database and
you should not need to do anything like this at all. You already have a
decoded string, because in python ALL strings are decoded already. Encoding
is only a process of converting strings to raw bytes for storage or
transmission, which you don't appear to be doing here.

On Thu, Feb 14, 2019 at 11:40 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?
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspealma at redhat.com  M: +1.336.210.5107
<https://red.ht/sig>
TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>



More information about the Python-list mailing list