【发布时间】:2020-07-19 01:53:23
【问题描述】:
我正在用 Flask 做一个项目,重点是: 在一条路线中,我生成一个 .csv 文件来下载它,其中包含来自 MySQL 的信息,如下所示 文件.csv
1,17.0,46.0,2.80696,0.559892,0.534016,0.543017,24.25,22:30:00,2020-04-28
2,17.0,47.0,2.80884,0.559142,0.534016,0.537016,24.5,23:00:00,2020-04-28
3,16.0,53.0,2.80921,0.547517,0.534016,0.537016,24.5,23:30:00,2020-04-28
4,16.0,49.0,2.80959,0.548079,0.534016,0.537016,24.25,00:00:00,2020-04-29
另一方面,我的服务器的其他路由可以选择上传文件,但上传文件时我可以看到纯文本: This is the output when upload my file
这个想法是从这个纯文本创建一个具有以下结构的列表:
dato[
[1,17.0,46.0,2.80696,0.559892,0.534016,0.543017,24.25,22:30:00,2020-04-28],
[2,17.0,47.0,2.80884,0.559142,0.534016,0.537016,24.5,23:00:00,2020-04-28],
[3,16.0,53.0,2.80921,0.547517,0.534016,0.537016,24.5,23:30:00,2020-04-28],
[4,16.0,49.0,2.80959,0.548079,0.534016,0.537016,24.25,00:00:00,2020-04-29]
]
我尝试了很多东西,但我做不到。 这是我的 app.py
@app.route('/upload',methods = ['POST'])
def upload_route_summary():
if request.method == 'POST':
# Create variable for uploaded file
f = request.files['fileupload']
#store the file contents as a string
fstring = f.read()
#create list of dictionaries keyed by header row
#csv_dicts = [{k: v for k, v in row.items()} for row in csv.DictReader(fstring.splitlines(), skipinitialspace=True)]
#do something list of dictionaries
return fstring
【问题讨论】: