Read CSV with csv Module

Parse a CSV file using Python's csv module.

Code

Python
import csv

with open("data.csv", "r", encoding="utf-8") as f:
    reader = csv.reader(f)
    header = next(reader)
    for row in reader:
        print(dict(zip(header, row)))

Line-by-line explanation

Expected output

{'name': 'John', 'age': '30'}

Related snippets

Related DuskTools