[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Convert a list with wrong encoding to utf8

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




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