Skip to content Skip to sidebar Skip to footer

Python Reading Specific Lines From Csv Using List Comprehension

Is it possible to make python read only chosen lines from a file? Let's say I've got a CSV file, file is separated by tab and the third column is either 'a', 'b' or 'c'. I would li

Solution 1:

Use the csv module:

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

Post a Comment for "Python Reading Specific Lines From Csv Using List Comprehension"