开发者

Python binary-data ascii error when encoding in multipart

I'm using this cookbook recipe to encode images in a multipart form data to upload to Twitter.

When I run the code to 开发者_C百科pack the image in a django shell everything runs fine (even the print statements that I used to debug the post body with binary data) but when I try to run the same script from a django Command or a much simpler pure-python script I keep getting this error:

body = '\r\n'.join(body)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)

So, why is this happening only when I run a script and not from the shell/CLI?

Thanks for everyone who can enlighten me into this :)


I think this is related to this question. Does the answer provided there help you?


I found out that it was related to the results of my query. They were returned in unicode and I used that value to create the path to my image, like:

image_path = "/my/path/%s.jpg" % model.name

model.name was something like u'model1' I printed out these values and removed the u' notation (by the old method of replacing :P) from the strings all worked fine.

What left me very frustrated is that the error pointed to '\r\n'.join and never to something related to the name of my file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜