UTF-8 problem encoding and decoding in Python3

Chris Rebert clp2 at rebertia.com
Sun Oct 10 21:08:52 CEST 2010


On Sun, Oct 10, 2010 at 10:25 AM,  <hidura at gmail.com> wrote:
> Hello everybody i am trying to encode a file string of an upload file and i
> am facing some problems with the first part of the file. When i open
> directly and try to decode the file the error is this: `UnicodeDecodeError:
> 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte`
> here is the first part of the file:
> `\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c`
> but when i try to encode the file in the server the encode change the parts
> of the file and the result is
> this:`\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10JFIF` without say that the
> file doesn 't save correctly.
>
> Any ideas?

Judging by the "\xff\xe0" and "JFIF", you're dealing with a JFIF file,
which is binary, and thus you shouldn't be encoding/decoding it in the
first place. Just write the byte string directly to a file (being sure
to include "b" in the `mode` argument to open() when opening the file)
without any further processing.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list