Loading Querying And Filtering Data Using The Csv Module

Reading CSV files

We can read a CSV file using the built-in csv module.

import csv

rows = []

f = open('file.csv')
reader = csv.DictReader(f)
print(reader.fieldnames) # shows all available fields

for row in reader:
  rows.append(row)
 
f.close()

# Now you can access your row data in `rows`

Note: You can only iterate through a reader once.

List Comprehensions

List comprehensions are expressions that allow you to create new lists out of existing lists using a mix of filter conditions and mapping conditions.

The basic syntax is something like...

Lambda functions

A lambda is a function expression: basically, a way to create functions inline.

Lambdas are especially useful if some function accepts a function as an argument. Common examples include sorted, min, and max

Last updated