Python CSV: Read and Write CSV files

1) Create & Write a Simple CSV

Create a CSV file data.csv with rows:

name,age
Alice,20
Bob,22

2) Read Entire CSV

Read data.csv from #1 and print each row.


3) Read CSV as List

Read data.csv and store all rows in a list, then print that list.


4) Write CSV with User Input

Ask the user to enter 3 names and ages and write them into users.csv.


5) Read Only First Column

Read users.csv and print only the name column.


6) Read Only Second Column

Read users.csv and print only the age column.


7) Append a Row

Open data.csv and append:


8) Count Rows

Read a CSV and print how many data rows it has (ignore header).


9) Write CSV with Headers

Create and write a CSV products.csv with headers:

and 3 product rows.


10) Read with DictReader

Use csv.DictReader to read products.csv and print each row as a dictionary.


11) Write with DictWriter

Ask the user for product data and write to products2.csv using csv.DictWriter and headers id,product,price.


12) Increase Prices

Read products.csv, increase all prices by 10%, and write results to a new CSV file.


13) Filter Rows

Read products.csv and print only rows where price > 50.


14) Save Filtered Rows

Read products.csv and write only rows with price > 50 to expensive.csv.


15) Convert CSV to List of Dicts

Read a CSV and convert it to a list of dictionaries, then print that list.


16) Sum Numeric Column

Read products.csv and compute the total of the price column.


17) Average Price

Read products.csv and print the average price of all items.


18) CSV to Text File

Read products.csv and write each row as a line in a text file like:


19) Check for Header

Write code that reads any CSV and prints:

if the first row contains any strings (not only numbers).


20) Merge Two CSV Files

Given two CSVs with the same headers, combine them into one CSV.


🧠 Concepts Practiced

Topic
What You Learn

Writing CSV

csv.writer, DictWriter

Reading CSV

csv.reader, DictReader

Headers

Use of first row as header

Filtering

Reading and deciding rows to keep

Aggregation

Summing/averaging numeric columns

Conversion

To lists, dictionaries, text format


📝 Starter Code Snippets

Writing a CSV

Reading a CSV

Using DictReader


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated