Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subscripting a specific line from python's csv reader?

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?

like image 472
ahhh Avatar asked Jul 09 '10 18:07

ahhh


1 Answers

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.

like image 140
jsbueno Avatar answered Sep 29 '22 17:09

jsbueno