Lesson 20 of 30
In Progress

Reading and Writing CSV files

CSV (Comma Separated Values) files are a common file format for storing and exchanging tabular data. CSV files are plain text files that use a comma to separate values, and each line in the file represents a row in the table.

Reading a CSV File

To read a CSV file in Python, you can use the “csv” module and the “DictReader” class. For example:

import csv

with open("filename.csv", "r") as file:
    reader = csv.DictReader(file)
    rows = list(reader)

In the above example, the “csv” module is imported, the “open” function is used to open the file “filename.csv” in read mode, and the “DictReader” class is used to create a reader object that reads the file into a list of dictionaries, with each dictionary representing a row in the table. The “with” statement is used to ensure that the file is properly closed after reading.

Writing to a CSV File

To write to a CSV file in Python, you can use the “csv” module and the “DictWriter” class. For example:

import csv

data = [
    {"name": "John Smith", "email": "john@example.com"},
    {"name": "Jane Doe", "email": "jane@example.com"}
]

with open("filename.csv", "w") as file:
    writer = csv.DictWriter(file, fieldnames=["name", "email"])
    writer.writeheader()
    writer.writerows(data)

In the above example, the “csv” module is imported, a list of dictionaries representing the data to be written to the file is defined, the “open” function is used to open the file “filename.csv” in write mode, and the “DictWriter” class is used to create a writer object that writes the data to the file. The “writeheader” method is used to write the field names as the first line of the file, and the “writerows” method is used to write the data rows to the file. The “with” statement is used to ensure that the file is properly closed after writing.

Conclusion

Overall, reading and writing CSV files in Python is a simple and straightforward task, and the “csv” module makes it easy to work with CSV files in your code. You can use the “DictReader” and “DictWriter” classes to read and write data in the form of dictionaries, or you can use the “reader” and “writer” classes to read and write data as lists of values.

Exercises

Here are some exercises with solutions to help you practice what you just learned:

How do you import the “csv” module in Python?

To import the “csv” module in Python, you can use the “import” statement. For example:

import csv

This will import the “csv” module and make it available for use in your code.

How do you open a CSV file in Python for reading?

To open a CSV file in Python for reading, you can use the “open” function followed by the “csv” module’s “DictReader” class. For example:

import csv

with open("filename.csv", "r") as file:
    reader = csv.DictReader(file)
    rows = list(reader)

In the above example, the “open” function opens the file “filename.csv” in read mode, and the “DictReader” class is used to create a reader object that reads the file into a list of dictionaries, with each dictionary representing a row in the table. The “with” statement is used to ensure that the file is properly closed after reading.

How do you open a CSV file in Python for writing?

To open a CSV file in Python for writing, you can use the “open” function followed by the “csv” module’s “DictWriter” class. For example:

import csv

data = [
    {"name": "John Smith", "email": "john@example.com"},
    {"name": "Jane Doe", "email": "jane@example.com"}
]

with open("filename.csv", "w") as file:
    writer = csv.DictWriter(file, fieldnames=["name", "email"])
    writer.writeheader()
    writer.writerows(data)

In the above example, the “open” function opens the file “filename.csv” in write mode, and the “DictWriter” class is used to create a writer object that writes the data to the file. The “writeheader” method is used to write the field names as the first line of the file, and the “writerows” method is used to write the data rows to the file. The “with” statement is used to ensure that the file is properly closed after writing.

How do you read a CSV file into a list of dictionaries in Python?

To read a CSV file into a list of dictionaries in Python, you can use the “csv” module’s “DictReader” class. For example:

import csv

with open("filename.csv", "r") as file:
    reader = csv.DictReader(file)
    rows = list(reader)

In the above example, the “open” function opens the file “filename.csv” in read mode, and the “DictReader” class is used to create a reader object that reads the file into a list of dictionaries, with each dictionary representing a row in the table. The “with” statement is used to ensure that the file is properly closed after reading.

How do you write a list of dictionaries to a CSV file in Python?

To write a list of dictionaries to a CSV file in Python, you can use the “csv” module’s “DictWriter” class. For example:

import csv

data = [
    {"name": "John Smith", "email": "john@example.com"},
    {"name": "Jane Doe", "email": "jane@example.com"}
]

with open("filename.csv", "w") as file:
    writer = csv.DictWriter(file, fieldnames=["name", "email"])
    writer.writeheader()
    writer.writerows(data)

In the above example, the “csv” module is imported, a list of dictionaries representing the data to be written to the file is defined, the “open” function is used to open the file “filename.csv” in write mode, and the “DictWriter” class is used to create a writer object that writes the data to the file. The “writeheader” method is used to write the field names as the first line of the file, and the “writerows” method is used to write the data rows to the file. The “with” statement is used to ensure that the file is properly closed after writing.