简体   繁体   English

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

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

I sent an excel file in a post request to my back-end and I tried handling the file using the following:我在发帖请求中向我的后端发送了一个 excel 文件,并尝试使用以下方法处理该文件:

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

However, the output from that is not an excel file.但是,其中的 output 不是 excel 文件。 It is a collection of XML files.它是 XML 文件的集合。 My end goal is to obtain a data frame from the file data that was sent so that I can extract the data.我的最终目标是从发送的文件数据中获取数据帧,以便我可以提取数据。

What is a clean way of handling this type of file?处理此类文件的干净方法是什么?

file_data = request.FILES[KEY].file

Returns the file uploaded in the form of bytes.以字节的形式返回上传的文件。 The following will read the excel file as a data frame:下面将excel文件作为数据帧读取:

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

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

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