Python:
Working with CSV

How to:

Python provides the built-in csv module to handle CSV files, making it straightforward to read from and write to them. For more robust and complex data manipulation, the third-party library pandas is highly popular.

Using the csv module

Reading a CSV file

import csv

with open('sample.csv', mode='r') as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        print(row)

Assuming sample.csv contains:

name,age,city
John,22,New York
Jane,28,Los Angeles

Output:

['name', 'age', 'city']
['John', '22', 'New York']
['Jane', '28', 'Los Angeles']

Writing to a CSV file

import csv

rows = [['name', 'age', 'city'], ['Jack', '33', 'Chicago'], ['Emily', '41', 'Denver']]

with open('output.csv', mode='w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(rows)

Creates or overwrites output.csv with:

name,age,city
Jack,33,Chicago
Emily,41,Denver

Using pandas for CSV

pandas is a powerful library for data manipulation that simplifies working with CSV files among other data formats.

Install pandas

pip install pandas

Reading a CSV file with pandas

import pandas as pd

df = pd.read_csv('sample.csv')
print(df)

Output:

    name  age         city
0   John   22    New York
1   Jane   28  Los Angeles

Writing to a CSV file with pandas

import pandas as pd

df = pd.DataFrame({'name': ['Jack', 'Emily'], 'age': [33, 41], 'city': ['Chicago', 'Denver']})
df.to_csv('output_pandas.csv', index=False)

Creates or overwrites output_pandas.csv with:

name,age,city
Jack,33,Chicago
Emily,41,Denver