python的from_bytes属性,在Python中打开csv文件:内建。 AttributeError AttributeError:'_io.BytesIO'对象没有属性'file'...

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.

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")

My flask/html code is as follows:

{% extends "layout.html" %}

`{% block title %}

Admin

{% endblock %}

{% block main %}

Admin Console

Upload Portfolio Data

data>

Select Portfolio Upload File

Upload Security Lists

Select Security Upload File

Upload

{% endblock %}

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)

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' object has no attribute 'file'

Anyone who can help my csv import nightmare to end?? Txs!!

解决方案

io.TextIOWrapper takes a io.BytesIO object all right.

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)

Just do:

csvfile = TextIOWrapper(request.files['portfolios'], encoding=request.encoding)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值