【发布时间】:2020-03-18 19:51:38
【问题描述】:
我正在尝试从 Python 请求中读取文件,形成数据。我要做的就是读入请求正文中的传入文件,解析内容并将内容作为 json 正文返回。我看到很多例子,比如:if 'filename' in request.files:,但这对我来说从来没有用过。我知道该文件实际上存在于 ImmutableMultiDict 类型中。这是我的工作代码示例:
if 'my_file.xls' in request.files:
# do something
else:
# return error
【问题讨论】:
-
ImmutableMultiDict([('', <FileStorage: 'my_file.xls' ('text/csv')>)])这是传入请求正文包含的内容 -
你能添加所有需要的代码来让你的程序运行吗?
-
from flask import request, Responsefrom create_app import my_appimport json@my_app.route('/api-examples/file_parser')def get_file():` if 'my_file.xls' in request.files:` ` # do something` ` else:` ` # return error`# main sectionif __name__ == '__main__':`my_app.run(host='0.0.0.0', port=5000)`