views:

22

answers:

1

i'd like to be able to access specific lines of a csv file through the csv reader. For example, the fourth line. Is there a way to do this with python's csv reader module?

+1  A: 

You just have to parse all the CSV file, and then use normal sequencing indexing.

Otherwise, you can do something like this

def my_filter(csv_file, lines):
   for line_number, line in enumerate(csv_file):
       if line_number in lines:
          yield line

my_file = open("file.csv")
my_reader = csv.reader(my_filter(my_file, (3,)))

Note that you can't avoid parsing the whole file, in a way or in another, because the lines are of variable lenght. The line count only advances when a '\n' is found, and it has to be found in a character by character basis.

Also, this filter won't work if you happen to have newline characters inside quotes in the csv file -- probably you are just better off parsing the whole file to a list, and retrieving the indexes from there, anyway:

my_file = open("file.csv")
my_reader = csv.reader(my_file)
my_line = list(my_reader)[3]
jsbueno
for sure. the file is just far too large to read the whole thing into memory at once. i plan on accessing all of the lines, but also need to jump around the file and have to avoid loading the whole thing at once.
ahhh
SO, the filter solution will help you - (as far as you don't have \n's inside literal values). I fyou need out of order access, the filter solution can be used to create a list like in the last example.
jsbueno