使用web.py上传Excel

我已经通过稍微修改文档中的示例尝试了下面的代码

class Upload(): def POST(self): web.header('enctype','multipart/form-data') print strftime("%Y-%m-%d %H:%M:%S", gmtime()) x = web.input(file={}) filedir = '/DiginUploads' # change this to the directory you want to store the file in. if 'file' in x: # to check if the file-object is created filepath=x.file.filename.replace('\\','/') # replaces the windows-style slashes with linux ones. filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension) fout = open(filedir +'/'+ filename,'w') # creates the file where the uploaded file should be stored fout.write(x.file.file.read()) # writes the uploaded file to the newly created file. fout.close() # closes the file, upload complete. 

但是这只适用于csv和txt文档。 对于Excel / pdf等文件被创build,但不能打开(损坏)。 我应该怎么做来处理这种情况?

我看到这个,但它是关于打印不涉及我的事情的内容。

打开文件时需要使用wb (二进制)模式:

 fout = open(filedir +'/'+ filename, 'wb')