[ale] [ot?] Python question

JK jknapka at kneuro.net
Mon Apr 2 13:04:27 EDT 2007


Michael B. Trausch wrote:

> I already asked the Python mailing list, and it seems that they are
> unwilling to help.  Basically, I have a situation wherein I have a
> binary file (a JPG in this case) that I want to add to a string which
> contains Unicode, in order to send to a server.  The server doesn't seem
> to accept Base64 (or quoted-printable, or any of that stuff), so I need
> to send the raw binary image to the server as part of the message.
> Python tells me that this is an error:
> 
> 
>>>>sb.UploadSinglePicture('/home/mbt/IMG_2618.JPG')
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "scrapbook.py", line 181, in UploadSinglePicture
>     {Request['UploadPic.Meta.Filename']: pic_mem})
>   File "scrapbook.py", line 237, in ComposeMIME
>     return(self.EncodeMIME(fields, files))
>   File "scrapbook.py", line 226, in EncodeMIME
>     body = eol.join(L)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0:
> ordinal not in range(128)
> 
> 
> I am aware that the binary file contains sequences that are neither
> ASCII nor UTF-8 or any other encoding.  That's not really the issue?I
> just need it to work.  Are there any ideas?  I have not, as of yet,
> found the answer on Google, though I have found tons of complaints
> regarding similar situations.

You might try reading this:

http://boodebr.org/main/python/all-about-python-and-unicode#UNI_TO_BINARY

As I understand it:

You need to encode the Unicode string before adding it to the
binary string.  A Unicode string in Python is actually an object of
class "unicode"; its encode() method will encode the string
according to a particular Unicode encoding, and produce a plain
Python binary string, which you can then join() to your existing
string without complaint.

-- JK




More information about the Ale mailing list