gpt4 book ai didi

python - 使用 web.py 上传 Excel

转载 作者:太空宇宙 更新时间:2023-11-04 05:33:44 24 4
gpt4 key购买 nike

我通过稍微修改 documentation 中的示例尝试了以下代码

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 等文件,已创建但无法打开(已损坏)。我应该怎么做才能处理这种情况?

我看到了this但它是关于打印的内容并没有解决我的问题。

最佳答案

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

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

关于python - 使用 web.py 上传 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36310286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com