【问题标题】:Exception has occurred: AttributeError '_csv.reader' object has no attribute 'shape'发生异常:AttributeError '_csv.reader' 对象没有属性 'shape'
【发布时间】:2018-12-03 23:07:35
【问题描述】:

我在 Python3 中收到以下错误: 发生异常:AttributeError '_csv.reader' 对象没有属性 'shape'

import requests

from contextlib import closing

import csv

url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv'

with closing(requests.get(url, stream=True)) as r:

    f = (line.decode('utf-8') for line in r.iter_lines())
    a = csv.reader(f, delimiter=',', quotechar='"')
    for row in a:
        print(row)

print(a.shape)

【问题讨论】:

  • 您可能会将csv 标准模块与pandas.read_csv 混淆,后者在后台使用Numpy 数组并公开.shape 属性。
  • 你尝试过什么来克服这个错误?你到底为什么要在那个对象上调用shape

标签: python-3.x


【解决方案1】:

csv.reader 类型没有.shape 属性。我通常会这样做

a = csv.reader(f, ...)
rows = list(a)
for row in rows:
    # ...
print(len(rows))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多