【问题标题】:How to handle excel file sent in POST request in Django?如何在 Django 中处理 POST 请求中发送的 excel 文件?
【发布时间】:2021-03-29 09:20:25
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python django pandas http-post


    【解决方案1】:
    file_data = request.FILES[KEY].file
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多