我想让我的用户上传一个csv文件,并在我的python flask后端中对其进行处理。如何将上传的csv文件传递到python flask后端?我目前有以下代码,在后端,我只是使用print查看数据,但它返回FileNotFoundError。另外,是否可以不使用with open方法?我应该在我的代码中确切更改什么?
HTML
<div class="col-sm-8 col-sm-offset-2">
<legend>
<h3>CSV to JSON<br>
<small>Files must be csv.</small></h3></legend>
<form align="center" action="/csv2json" method="post" enctype="multipart/form-data" class="form-horizontal">
<fieldset>
<div class="form-group">
<label class="col-sm-2 control-label">File</label>
<div class="col-sm-5">
<input type="file" name="data_file" placeholder="file" class="form-control" required="true">
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-2">
<input type="submit" class="btn btn-primary form-control" />
</div>
</div>
</fieldset>
</div>
</form>
Python后端
@app.route('/csv2json', methods=["POST"])
def convert():
f = request.files['data_file']
with open(f.filename, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter='')
for row in csvreader:
print(', '.join(row))