簡體   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