繁体   English   中英

如何处理 Django 中 POST 请求中发送的 excel 文件?

[英]How to handle excel file sent in POST request in Django?

我在发帖请求中向我的后端发送了一个 excel 文件,并尝试使用以下方法处理该文件:

def __handle_file(file):
destination = open('./data.xslx', 'wb')
for chunk in file.chunks():
    destination.write(chunk)
destination.close()

但是,其中的 output 不是 excel 文件。 它是 XML 文件的集合。 我的最终目标是从发送的文件数据中获取数据帧,以便我可以提取数据。

处理此类文件的干净方法是什么?

file_data = request.FILES[KEY].file

以字节的形式返回上传的文件。 下面将excel文件作为数据帧读取:

data = pd.read_excel(io.BytesIO(file_data.read()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM