UnicodeDecodeError

thomas ct adu_thoma at yahoo.com
Thu May 5 16:03:44 EDT 2005


Hi Fredrik

 

Sorry that I confused u 
Thomas Thomas wrote:

>there's no way the tuple creation will generate a UnicodeDecodeError
>all by itself.  are you sure the error occurs on that line?

u r right, the error is not generated in tuple creation . it generated where join the strings at the line 
 
body = CRLF.join(L)

what i was trying to say was how the various tuples behave..if i use
 
file = ('file', filename, data)  #it gives me unicode error
in this case I am using binary data..
 
file = ('file', filename, 'data'*100)
but the same filename works fine if i pass text data
 
file = ('file', 'test.pdf', data)
 
so if hardcode the file name /use 
filename=filename.encode("ascii") things work with binary data as well..
 
I was wondering why..
 
thanks 
Thomas

import httplib, mimetypes
import os;

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'



filepath= 'c:/Documents and Settings/Administrator/Desktop/tmp/test.pdf';
f = open(filepath, "rb")
data = f.read()
f.close()

(filedir, filename) = os.path.split(filepath)
filename=unicode(filename);
#file = ('file', filename, data)  #This won't work will give a UnicodeDecodeError: 'ascii' codec can't decode byte 0xc7 in position 10: ordinal not in range(128)
file = ('file', filename, 'data'*100) #this will work fine
#file = ('file', 'test.pdf', data) #This also works fine
files = [file]
CRLF = '\r\n'
L = []
for (key, filename, value) in files:
    L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
    L.append(value)
body = CRLF.join(L)




		
---------------------------------
Do you Yahoo!?
 Yahoo! Mail - Find what you need with new enhanced search. Learn more.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050505/cebe728b/attachment.html>


More information about the Python-list mailing list