I'd like to read at most 20 lines from a csv file:
rows = [csvreader.next() for i in range(20)]
Works fine if the file has 20 or more rows, fails with a StopIteration exception otherwise.
Is there an elegant way to deal with an iterator that could throw a StopIteration exception in a list comprehension or should I use a regular for loop?