在Python中打开csv文件:内置文件. AttributeError AttributeError:'_io.BytesIO'对象没有属性'file' [英] Opening csv file in Python: builtins. AttributeError AttributeError: '_io.BytesIO' object has no attribute 'file'
问题描述
关于我先前的问题,关于如何在Python中打开csv文件,我仍然没有成功,而且错误不断.
further to my earlier question, on how to open an csv file in Python, I am still not successful in doing so and going from error to error.
我的Python代码如下:
My Python code 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
csvfile = TextIOWrapper(request.files['portfolios'].file, encoding=request.encoding)
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")
我的flask/html代码如下:
My flask/html code is as follows:
{% 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 %}
最初,我从字面上遵循了Python文档中的示例: 导入csv 使用open('names.csv')作为csvfile: reader = csv.DictReader(csvfile) 对于阅读器中的行: print(row ['first_name'],row ['last_name'])
Initially, I literally followed the example from the Python documentation: import csv with open('names.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: print(row['first_name'], row['last_name'])
this didnt work as it gave a type error (see my earlier post)
然后,我按照建议删除了打开",这导致了另一个错误.然后,我将整个with块删除,这又导致了错误.现在,上面的代码就是我现在的代码,它会产生以下错误:
I then removed, as suggested, the "open", which resulted in another error. I then removed the whole with block, which again resulted in an error. Now, the above code is what I am now, and its generating the following error:
builtins.AttributeError AttributeError:'_io.BytesIO'对象没有属性'file'
builtins.AttributeError AttributeError: '_io.BytesIO' object has no attribute 'file'
可以帮助我的csv导入噩梦结束的任何人吗?谢谢!!
Anyone who can help my csv import nightmare to end?? Txs!!
推荐答案
io.TextIOWrapper
接受io.BytesIO
对象.
(几乎)将其传递,除了要添加.file
(为什么?),它不是io.BytesIO
类的字段(request.files['portfolios']
已经是io.BytesIO
对象) )
You're (almost) passing it, except that you're adding a .file
(why??), which is not a field of the io.BytesIO
class (request.files['portfolios']
is a io.BytesIO
object already)
只需:
csvfile = TextIOWrapper(request.files['portfolios'], encoding=request.encoding)
这篇关于在Python中打开csv文件:内置文件. AttributeError AttributeError:'_io.BytesIO'对象没有属性'file'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!