Lesson 11.4: Working with CSV Data
What you will learn:
- What CSV files are and why they are useful
- How to read CSV files with
csv.reader - How to write CSV files with
csv.writer - Using
DictReaderandDictWriterfor column-based access - Practical examples with real-world-style data
1. What Are CSV Files?
CSV stands for Comma-Separated Values. It is one of the most common formats for storing tabular data (data organized in rows and columns). You can open CSV files in spreadsheet programs like Excel or Google Sheets.
Example: students.csv
name,age,grade Alice,20,A Bob,22,B Charlie,21,A Diana,23,B+
Python's built-in csv module handles the tricky parts of reading and writing CSV files, like dealing with commas inside quoted values.
2. Reading CSV Files with csv.reader
The csv.reader reads each row as a list of strings:
Basic CSV Reading
import csv with open("students.csv", "r") as file: reader = csv.reader(file) # Read the header row first header = next(reader) print("Columns:", header) # Read each data row for row in reader: print(row)
['Alice', '20', 'A']
['Bob', '22', 'B']
['Charlie', '21', 'A']
['Diana', '23', 'B+']
Accessing Individual Values
import csv with open("students.csv", "r") as file: reader = csv.reader(file) next(reader) # Skip header for row in reader: name = row[0] age = row[1] grade = row[2] print(f"{name} is {age} years old with grade {grade}")
Bob is 22 years old with grade B
Charlie is 21 years old with grade A
Diana is 23 years old with grade B+
3. Writing CSV Files with csv.writer
Writing a CSV File
import csv # Data to write students = [ ["Alice", 95, "A"], ["Bob", 87, "B+"], ["Charlie", 92, "A-"], ["Diana", 78, "C+"] ] with open("grades.csv", "w", newline="") as file: writer = csv.writer(file) # Write the header writer.writerow(["Name", "Score", "Grade"]) # Write all data rows writer.writerows(students) print("CSV file created!")
newline="" in the open() call. This prevents extra blank lines from appearing between rows on Windows.
4. DictReader: Access Columns by Name
DictReader reads each row as a dictionary, using the header row as keys. This makes your code much more readable:
Reading with DictReader
import csv with open("students.csv", "r") as file: reader = csv.DictReader(file) for row in reader: # Access values by column name! print(f"{row['name']} - Age: {row['age']}, Grade: {row['grade']}")
Bob - Age: 22, Grade: B
Charlie - Age: 21, Grade: A
Diana - Age: 23, Grade: B+
Using row['name'] is much clearer than row[0] because you do not have to remember which column number corresponds to which data.
5. DictWriter: Write with Column Names
Writing with DictWriter
import csv students = [ {"name": "Alice", "score": 95, "grade": "A"}, {"name": "Bob", "score": 87, "grade": "B+"}, {"name": "Charlie", "score": 92, "grade": "A-"} ] with open("results.csv", "w", newline="") as file: fieldnames = ["name", "score", "grade"] writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writeheader() # Write the column names writer.writerows(students) # Write all rows print("Results saved!")
6. Real-World Example: Analyzing Data
Finding the Highest Score
import csv highest_score = 0 top_student = "" with open("grades.csv", "r") as file: reader = csv.DictReader(file) for row in reader: score = int(row["Score"]) if score > highest_score: highest_score = score top_student = row["Name"] print(f"Top student: {top_student} with {highest_score} points")
Check Your Understanding
What is the advantage of using DictReader over csv.reader?
DictReader lets you access values by column name (e.g., row['name']) instead of by index number (e.g., row[0]). This makes your code more readable and less error-prone, especially when working with files that have many columns.
Key Takeaways
- CSV (Comma-Separated Values) is a common format for tabular data
csv.readerreads rows as lists;DictReaderreads rows as dictionariescsv.writerwrites lists;DictWriterwrites dictionaries- Use
next(reader)to skip the header row when usingcsv.reader - Always use
newline=""when opening files for CSV writing - CSV values are always read as strings - convert to int/float when needed