Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

python - Stop Iteration error when using next()

I am not able to clarify my self over the use of next() in python(3).

I have a data :

chr pos ms01e_PI    ms01e_PG_al ms02g_PI    ms02g_PG_al ms03g_PI    ms03g_PG_al ms04h_PI    ms04h_PG_al
2   15881989    4   C|C 6   A|C 7   C|C 7   C|C
2   15882091    4   A|T 6   A|T 7   T|A 7   A|A
2   15882148    4   T|T 6   T|T 7   T|T 7   T|G

and I read it like:

Works fine

c = csv.DictReader(io.StringIO(data), dialect=csv.excel_tab)
print(c)
print(list(c))

Works fine

c = csv.DictReader(io.StringIO(data), dialect=csv.excel_tab)
print(c)
keys = next(c)
print('keys:', keys)

But, now there is a problem.

c = csv.DictReader(io.StringIO(data), dialect=csv.excel_tab)
print(c)
print(list(c))
keys = next(c)
print('keys:', keys)

Error message:

Traceback (most recent call last):
2   15882601    4   C|C 9   C|C 6   C|C 5   T|C

  File "/home/everestial007/Test03.py", line 24, in <module>
keys = next(c)
  File "/home/everestial007/anaconda3/lib/python3.5/csv.py", line 110, in __next__

    row = next(self.reader)

StopIteration

Why does print(keys) after print(list(c)) gives StopIteration? I read the documentation but I am not clear on this particular example.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error isn't with the print statement. It's with the keys = next(c) line. Consider a simpler example which reproduces your issue.

a = (i ** 2 for i in range(5))  

a           # `a` is a generator object
<generator object <genexpr> at 0x150e286c0>

list(a)     # calling `list` on `a` will exhaust the generator
[0, 1, 4, 9, 16]

next(a)     # calling `next` on an exhausted generator object raises `StopIteration`
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-2076-3f6e2eea332d> in <module>()
----> 1 next(a)

StopIteration: 

What happens is that c is an iterator object (very similar to the generator a above), and is meant to be iterated over once until it is exhausted. Calling list on this object will exhaust it, so that the elements can be collected into a list.

Once the object has been exhausted, it will not produce any more elements. At this point, the generator mechanism is designed to raise a StopIteration if you attempt to iterate over it even after it has been exhausted. Constructs such as for loops listen for this error, silently swallowing it, however, next returns the raw exception as soon as it has been raised.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...