I have a csv.reader
reading a file, but repeatedly reading the same line.
import csv
with open('mydata.csv', 'rb') as f:
reader = csv.reader(f)
reader.next()
for row in reader:
while i < 10:
print row
i=i+1
The code prints the second row (as I want to skip the header) 10 times.
Your code is doing exactly what you told it to do... (and also, your title is misleading: the reader is reading the row only once, you are simply printing it 10 times)
reader.next() # advances to second line
for row in reader: # loops over remaining lines
while i < 10: # loops over i
print row # prints current row - this would be the second row in the first forloop iteration... 10 times, because you loop over i.
i=i+1 # increments i, so the next rows, i is already >=10, your while-loop only affects the second line.
Why do you have that while loop in the first place? You could easily do something like:
reader = csv.reader(f)
for rownum, row in enumerate(reader):
if rownum: #skip first line
print row
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.