8

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?

ahhh
  • 123
  • 1
  • 2
  • 5

1 Answers1

4

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]

update Most important: if you need random access to information which is far too large to fit in memory, just consider dumping it to a SQL database instead. It will spare one reinventing a lot of wheels.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 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 Jul 09 '10 at 18:31
  • 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 Jul 09 '10 at 22:29