file io - python reading specific lines from CSV using list comprehension -


is possible make python read chosen lines file?

let's i've got csv file, file separated tab , third column either 'a', 'b' or 'c'. have list comprehension (or generator, doesn't matter) return lines in file have chosen first column

the following throws syntax error:

lines = [tmp = line.rstrip().split(separator_column) line in source if tmp[2] == 'a'] 

is possible in more pythonic way for-loop? called more pythonic ways working speed of c - they're faster basic python instructions - that's why ask.

use csv module:

import csv open("your/file.csv", ...) source:     reader = csv.reader(source, delimiter='\t')     selection = [row row in reader if row[2] == 'a'] 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -