python - 通过 Django 读取 xlsx 上传
问题描述
我正在通过我的网站上传文件(xlsx),我认为这些文件已正确上传。
def upload_file(request):
print('here',request,request.method,request.FILES)
if request.method == 'POST':
file_json= convert_xlsx_to_json(request.FILES['file_query'])
return render(request, 'drax_internal_dbs/drax_success_reactions.html', {'xlsx_file':file_json})
return render(request, 'drax_internal_dbs/no_results.html')
这是我的文件:
<WSGIRequest: POST '/drax_output.html'> POST <MultiValueDict: {'file_query': [<InMemoryUploadedFile: calcitriol_2.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)>]}>
当我尝试读取文件时,我遇到了很多问题。我正在使用 .read() 获取数据并希望将其转换为数据帧。问题出现在这里,我不知道如何转换数据?我一直在谷歌搜索,但仍然找不到解决方案。这是我到目前为止所尝试的:
- 将文件转换为 StringIO 并使用 openpyxl.load_workbook
- 将文件转换为 BytesIO 并使用 openpyxl.load_workbook
- 使用 pandas.read_csv(file_contents=file.read())
可能还有其他我现在不记得的方式。我不想保存文件,因为我只会用它来生成 HTML 表格。
谢谢!
解决方案
我没有找到 xlsx 的答案。相反,我转而使用 csv 文件作为输入文件,它几乎可以立即工作。
推荐阅读
- apache-spark - Azure databricks spark - 写入 blob 存储
- c# - EF Core 如何根据对象属性更新实体
- kubernetes - kubeadm 生产现在准备好了吗?
- laravel - Laravel 论坛示例
- javascript - 如何使用 ChartJS 清楚地显示条形图标签?
- python - 在 Python 中模拟蛇和梯子游戏时的问题
- firebase - 如何将来自 pushnotification 的多条消息合并为 reactnative firebase 中的一条消息
- hyperledger-fabric - Hyperledger Composer 上的文件服务器?
- python - 带有字符输入的keras RNN无法预测
- ios - 如何在 Xcode 10.0 的 UITableView 中添加原型单元格