Hi Jessica, In the original code you posted it looks like you had already saved the file to disk before you went about trying to print it. Did this result in a valid file that you could open in excel?
Regards, Kevin On Tue, Feb 11, 2014 at 12:21 AM, Jessica Le <[email protected]> wrote: > I dont quite understand why I would want to encode/decode the xls files > either, but for some reason the upload method from the webpy library throws > the UnicodeDecodeError when I try to print out the values from the uploaded > Excel file. The upload file method works for text files fine though. I > investigated the UnicodeDecode error on stackoverflow, and it seemed like > that was the way to go... > > All I want to do is let the user be able to upload an Excel file using > REST. It shouldn't be this hard :( > > > On Mon, Feb 3, 2014 at 7:58 PM, James Tyra <[email protected]> wrote: > >> Jessica just so you know I can not possibly imagine a reason to run >> pythons "encode" or "decode" functions on an uploaded Excel file or ANY >> file. These functions interpret the input as a text stream, modifying it in >> various ways to make it human readable. This will absolutely corrupt an >> excel file or any non txt file. >> >> In order to process Microsoft xls or xlsx files with Python you are going >> to need some type of special library. >> >> I recommend you take a step back for a second and explain to us what your >> trying to accomplish. >> On Feb 3, 2014 8:52 PM, "Jessica Le" <[email protected]> wrote: >> >>> So after doing more research/testing, it seems like the built in >>> function for file upload only seems to work flawlessly for txt files. Is >>> there a library out there for other kinds of files? >>> >>> On Friday, January 17, 2014 3:58:15 PM UTC-6, Jessica Le wrote: >>>> >>>> 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) Python >>>> C:\Users\jl98567\Documents\xMatters_calendar_app\schedule_web.py in >>>> POST, line 45 WebPOST 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! >>>> >>>> -- >>> 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. >>> >> -- >> You received this message because you are subscribed to a topic in the >> Google Groups "web.py" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/webpy/_i64Ym_Ubxg/unsubscribe. >> To unsubscribe from this group and all its topics, 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. >> > > -- > 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. > -- 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.
