使用Flask在Python中打开csv文件 [英] Opening csv file in Python using Flask
本文介绍了使用Flask在Python中打开csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何想法我做错了什么?
我的Python代码/ Flash路由如下:
app.route(/ admin,methods = [GET,POST])
@login_required
def admin():
配置管理屏幕
#如果用户通过POST达到路由(如通过POST提交表单)
如果request.method ==POST:
#用组合数据加载csv文件
with open(request.files [portfolio])as csvfile:
portfolio = csv.DictReader(csvfile)
#在字典中加载csv文件
用于行投资组合:
print(row ['first_name'],row ['last_name'])
else:
return render_template(admin.html)
$ c $我的html / Flask代码是: > {%extendslayout.html%}
{%block title%}
管理员
{%endblock%}
{%block main%}
< h2>管理控制台< / h2>
< h3>上传投资组合数据< / h2>
< form action ={{url_for('admin')}}method =postenctype = multipart / form-data>
< fieldset>
< label class =control-label>选择投资组合上传文件< / label>
< input id =input-1type =fileclass =filename =portfolio>
< h3>上载安全清单< / h2>
< label class =control-label>选择安全上传文件< / label>
< input id =input-1type =fileclass =filename =securities>
< div class =form-group>
< button class =btn btn-defaulttype =submitvalue =upload>上传< / button>
< / div>
< / fieldset>
< / form>
{%endblock%}
解决方案已经打开。 打开
需要一个字符串文件名,并创建一个打开的文件对象,但不需要这样做,因为 request.files $ c
portfolio = csv.DictReader(request.files ['portfolio'])
So, I am trying to open an .csv file in Python using Flask. I copies the code from the Python library, but I go from one error message to the other and I don't know what I am doing wrong. The latest error code I get on the below code is: TypeError: invalid file:
Any ideas what I am doing wrong?
My Python code/Flash route is as follows:
@app.route("/admin", methods=["GET", "POST"])
@login_required
def admin():
"""Configure Admin Screen"""
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# load csv file with portfolio data
with open(request.files["portfolios"]) as csvfile:
portfolios = csv.DictReader(csvfile)
# load csv file in dictionary
for row in portfolios:
print(row['first_name'], row['last_name'])
else:
return render_template("admin.html")
My html/Flask code is:
{% extends "layout.html" %}
{% block title %}
Admin
{% endblock %}
{% block main %}
<h2>Admin Console</h2>
<h3> Upload Portfolio Data</h2>
<form action="{{ url_for('admin') }}" method="post" enctype=multipart/form-data>
<fieldset>
<label class="control-label">Select Portfolio Upload File</label>
<input id="input-1" type="file" class="file" name="portfolios">
<h3>Upload Security Lists</h2>
<label class="control-label">Select Security Upload File</label>
<input id="input-1" type="file" class="file" name="securities">
<div class="form-group">
<button class="btn btn-default" type="submit" value = "upload">Upload</button>
</div>
</fieldset>
</form>
{% endblock %}
解决方案 The file is already open. open
takes a string filename and created an open file object, but you don't need to do that because objects in request.files
are already open file-like objects.
portfolios = csv.DictReader(request.files['portfolios'])
这篇关于使用Flask在Python中打开csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文