Hi Hugo! Thanks for your reply.
I tried them all, but none of the encoding and decoding methods work. Still
receiving the Unicode error. If I try the
print x['calendar_file'].value
it works, but it doesn't translate it correctly. It just prints out a bunch
of garbage.
On Friday, January 17, 2014 4:15:09 PM UTC-6, Hugo Lol wrote:
>
>
>
> El viernes, 17 de enero de 2014 18:58:15 UTC-3, Jessica Le escribió:
>>
>> Hi all,
>>
>> I'm trying to create an application that lets users upload an .xls file
>> that I then take and feed that uploaded.xls file into my program which
>> reads and parses it. However, I am having issues with the utf-8 encoding
>> for the Excel files. I have searched everywhere on stackoverflow and
>> google, but none of them are working.
>>
>> Here is my code:
>>
>> def POST(self):
>> x = web.input(calendar_file={}, ref_id='')
>> if x:
>> ref_id = (x.ref_id if x.ref_id else "")
>> filepath=x.calendar_file.filename # replaces the windows-style
>> slashes with linux ones.
>> fn=filepath.split('/')[-1] # splits the and chooses the last
>> part (the filename
>> filename =
>> "%s/Users/jl98567/Documents/xMatters_calendar_app/test/" + fn
>> fullpath = os.path.join('c:', filename % (ref_id))
>> content = x["calendar_file"].file.read()
>> with open(fullpath, 'w') as f_out:
>> if not f_out:
>> raise Exception("Unable to open %s for writing. " % (fullpath))
>> f_out.write(content)
>> print str(x['calendar_file'].value.encode('utf8','ignore'))
>> raise web.seeother('/upload?ref_id=%s&filename=%s' % (ref_id,
>> filename))
>>
>>
>> Here is the error;
>>
>> <type 'exceptions.UnicodeDecodeError'> at /'ascii' codec can't decode
>> byte 0xd0 in position 0: ordinal not in
>> range(128)PythonC:\Users\jl98567\Documents\xMatters_calendar_app\schedule_web.py
>>
>> in POST, line 45WebPOST http://localhost:8080/
>> line 45 is: print str(x['calendar_file'].value.encode('utf8','ignore'))
>>
>> I have tried decoding it and then encoding it similar to this one:
>>
>> print
>> str(x['calendar_file'].value.decode('utf-8').encode('utf8','ignore'))
>>
>> but still doesn't work.
>>
>> Any suggestions?
>>
>> Thanks much!
>>
>>
> What if you just do this:
>
> print x['calendar_file'].value
>
> Also, why are you converting that to str after encoding to UTF-8? The str
> type only works with 7-bit ASCII char codes. Try removing the str conversion.
> Also, try decoding instead of coding:
> x['calendar_file'].value.decode("utf-8").
>
> This is the bad face of Python 2, the UTF-8 errors and all that stuff.
>
> Hope it helps!
>
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/groups/opt_out.