How do I encode and decode this data to write to a file?
Terry Jan Reedy
tjreedy at udel.edu
Mon Apr 29 18:02:08 EDT 2013
On 4/29/2013 5:47 AM, cl at isbd.net wrote:
> case). Here's the traceback:-
>
> File "/usr/local/lib/python2.7/dist-packages/gallery/picture.py", line 361,
> in createPictureHTML file.write("".join(html).encode('utf-8'))
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
783: ordinal not in range(128)
Generiric advice for anyone getting unicode errors:
unpack the composition producing the error
so that one can see which operation produced it.
In this case
s = "".join(html)\
s = s.encode('utf-8')
file.write(s)
This also makes it possible to print intermediate results.
print(type(s), s) # would have been useful
Doing so would have immediately shown that in this case the error was
the encode operation, because s was already bytes.
For many other posts, the error with the same type of message has been
the print or write operation, do to output encoding issues, but that was
not the case here.
More information about the Python-list
mailing list