7

I started to use the csv.reader in Python 2.6 but you can't use len on it, or slice it, etc. What's the reason behind this? It certainly feels very limiting.

Or is this just an abandoned module in later versions?

1 Answer 1

14

I'm pretty sure you can't use len or slice because it is an iterator. Try this instead.

import csv
r = csv.reader(...)
lines = [line for line in r]
print len(lines) #number of lines
for odd in lines[1::2]: print odd # print odd lines
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I didn't know it was an iterator. I thought it was a multi dimensional list.
You can replace lines = [line for line in r] with lines = list(r)
You can use itertools.islice if you need to slice an iterator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.