我仍然在范围内得到IndexError [英] I get IndexError while still in the range

查看:139
本文介绍了我仍然在范围内得到IndexError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取csv文件的行。我的档案看起来像这样

I am trying to read the rows of a csv file. My file looks like this

Col 1, Col 2, Col3
row11, row12, row13
row21, row22, row23
row31, row32, row33
...



使用以下命令读取

I use the following command to read the rows

with open('~/data.csv') as f:
    r = csv.DictReader(f)
    for i in range(5):
        print(list(r)[i])

输出打印第一行,但是后面会出现索引错误。

The output prints the first row, but then it give the out of index error right after.

IndexError                                Traceback (most recent call last)
<ipython-input-15-efcc4f8c760d> in <module>()
      2     r = csv.DictReader(f)
      3     for i in range(5):
----> 4         print(list(r)[i])

IndexError: list index out of range

我猜我在某个地方犯了一个愚蠢的错误,但不能发现它。关于我在做什么以及如何解决它的任何想法?

I'm guessing I'm making a silly mistake somewhere, but can't spot it. Any ideas on what I am doing wrong and how to fix it?

编辑:这是 print (list(r))

[{'Col 1': 'row11', ' Col3': ' row13', ' Col 2': ' row12'}, {'Col 1': 'row21', ' Col3': ' row23', ' Col 2': ' row22'}, {'Col 1': 'row31', ' Col3': ' row33', ' Col 2': ' row32'}, {'Col 1': 'row41', ' Col3': ' row43', ' Col 2': ' row42'}, {'Col 1': 'row51', ' Col3': ' row53', ' Col 2': ' row52'}, {'Col 1': 'row61', ' Col3': ' row63', ' Col 2': ' row62'}, {'Col 1': 'row71', ' Col3': ' row73', ' Col 2': ' row72'}, {'Col 1': 'row81', ' Col3': ' row83', ' Col 2': ' row82'}, {'Col 1': 'row91', ' Col3': ' row93', ' Col 2': ' row92'}, {'Col 1': 'row101', ' Col3': ' row103', ' Col 2': ' row102'}]


推荐答案

DictReader(f)只是让你一次看看你的文件 - 你只能调用 code>一次,但是你多次调用它,因为它在循环中。稍后调用返回一个空的列表。只要在循环之外调用 list ,并将其保存在一个变量中即可。

DictReader(f) just gives you a one time look at your file -- you can only call list on it once, but you call it multiple times because it's within the loop. Later calls return an empty list. Just call list on it outside of the loop and save it in a variable, and you'll be golden.

这是:

r = csv.DictReader(f)
rows = list(r)
for i in range(5):
    print(rows[i])

不要在任何时候将整个内容写入内存:

Or, don't pull the whole thing into memory at any point:

for row in csv.DictReader(f):
    print row

如果您想为其他用途保留索引:

If you'd like to keep the index around for other purposes:

 for i, row in enumerate(csv.DictReader(f)):
     print i, row

如果你想从迭代器 $ c> csv.DictReader 是一个特殊的情况)没有把整个事情拉到内存中,请在检查 itertools.islice https://docs.python.org/3/library/itertools.html =nofollow> https://docs.python.org/3/library/itertools.html 。它基本上允许列表迭代器上切分。

If you want to get specific rows from an iterator (which csv.DictReader is a special case of) without pulling the whole thing into memory, check out itertools.islice at https://docs.python.org/3/library/itertools.html. It basically allows list-style slicing on an iterator.

  # prints first five rows
  for row in itertools.islice(csv.DictReader(f), 5):
       print row

对于更多的散发行:

  needed_row_indices = {2, 5, 20}
  for i, row in enumerate(csv.DictReader(f)):
      if i in needed_row_indices:
          print row

这篇关于我仍然在范围内得到IndexError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆