Python csv Module
CSV (Comma-Separated Values) files are a common file format used to store tabular data.
CSV files consist of plain text, where each row represents a row of data in the table, and each column is separated by commas (or other delimiters).
CSV files are commonly used for data exchange because they are simple and easy to process.
Python provides a built-incsvmodule for reading and writing CSV files. This module simplifies the process of handling CSV files, allowing developers to easily manipulate tabular data.
1. Reading CSV Files
To read CSV files, you can use thecsv.readerobject. Here is a simple example:
Example
# Open the CSV file
with open('data.csv', mode='r', encoding='utf-8') as file:
# Create a csv.reader object
csv_reader = csv.reader(file)
# Read data row by row
for row in csv_reader:
print(row)
Code Explanation:
open('data.csv', mode='r', encoding='utf-8'): In read-only mode, open the file nameddata.csvand specify the encoding as UTF-8.csv.reader(file): Create acsv.readerobject to read the file content.for row in csv_reader: Reads the file content row by row, and each row of data is parsed into a list.
2. Writing CSV Files
To write to CSV files, you can use thecsv.writerobject. Here is an example:
Example
# Data to be written
data = [
['Name', 'Age', 'City'],
['Alice', '30', 'New York'],
['Bob', '25', 'Los Angeles']
]
# Open the CSV file
with open('output.csv', mode='w', encoding='utf-8', newline='') as file:
# Create a csv.writer object
csv_writer = csv.writer(file)
# Write data
for row in data:
csv_writer.writerow(row)
Code Explanation:
open('output.csv', mode='w', encoding='utf-8', newline=''): In write mode, open the file namedoutput.csvand specify the encoding as UTF-8.newline=''Used to avoid blank lines in Windows systems.csv.writer(file): Create acsv.writerobject to write the file content.csv_writer.writerow(row): Writes each row of data to the file.
3. Reading and Writing CSV Files Using Dictionaries
csvThe module also provides theDictReaderandDictWriterclasses, which can parse each row of a CSV file into a dictionary, or write dictionaries to a CSV file.
UsingDictReaderto read CSV files:
Example
with open('data.csv', mode='r', encoding='utf-8') as file:
csv_dict_reader = csv.DictReader(file)
for row in csv_dict_reader:
print(row)
UsingDictWriterto write CSV files:
Example
data = [
{'Name': 'Alice', 'Age': '30', 'City': 'New York'},
{'Name': 'Bob', 'Age': '25', 'City': 'Los Angeles'}
]
with open('output.csv', mode='w', encoding='utf-8', newline='') as file:
fieldnames = ['Name', 'Age', 'City']
csv_dict_writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write the header
csv_dict_writer.writeheader()
# Write data
for row in data:
csv_dict_writer.writerow(row)
Common Attributes and Methods
Core Methods of the csv Module
| Method | Description | Example |
|---|---|---|
csv.reader() | Read CSV data from a file object | reader = csv.reader(file) |
csv.writer() | Write data to a CSV file | writer = csv.writer(file) |
csv.DictReader() | Read CSV rows as dictionaries (with header) | dict_reader = csv.DictReader(file) |
csv.DictWriter() | Write dictionaries to a CSV file (field names required) | dict_writer = csv.DictWriter(file, fieldnames) |
csv.register_dialect() | Register a custom CSV format (e.g., delimiter) | csv.register_dialect('mydialect', delimiter='|') |
csv.unregister_dialect() | Delete a registered dialect | csv.unregister_dialect('mydialect') |
csv.list_dialects() | List all registered dialects | print(csv.list_dialects()) |
Common Methods of csv.reader and csv.writer Objects
| Method | Description | Applicable Object |
|---|---|---|
__next__() | Iteratively read the next row (or use aforloop) | reader |
writerow(row) | Write a single row of data | writer |
writerows(rows) | Write multiple rows of data (a list of lists) | writer |
Features of csv.DictReader and csv.DictWriter Objects
| Feature/Method | Description | Example |
|---|---|---|
fieldnames | Field name list (DictReaderautomatically obtained from the first row) | dict_reader.fieldnames |
writeheader() | Write the header row (DictWriterexclusive) | dict_writer.writeheader() |
Common Parameter Descriptions
| Parameter | Description | Example Value | Applicable Method |
|---|---|---|---|
delimiter | Field delimiter | ','(default)'\t' | reader/writer |
quotechar | Quote character (encloses special fields) | '"'(default) | reader/writer |
quoting | Quoting rule | csv.QUOTE_ALL(quote all) | reader/writer |
skipinitialspace | Ignore spaces after the delimiter | True/False | reader |
lineterminator | Line terminator | '\r\n'(default) | writer |
dialect | Predefined dialect name | 'excel'(default) | All methods |
Example
1. Reading CSV Files
Example
with open('data.csv', 'r') as file:
reader = csv.reader(file, delimiter=',')
for row in reader:
print(row) # Each row is a list
2. Writing CSV Files
Example
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data) # Write multiple rows
3. Using DictReader and DictWriter (with header)
Example
with open('data.csv', 'r') as file:
dict_reader = csv.DictReader(file)
for row in dict_reader:
print(row['Name'], row['Age']) # Access via field names
# Write
fieldnames = ['Name', 'Age']
with open('output.csv', 'w', newline='') as file:
dict_writer = csv.DictWriter(file, fieldnames=fieldnames)
dict_writer.writeheader() # Write the header
dict_writer.writerow({'Name': 'Alice', 'Age': 25})
4. Custom Dialects (e.g., handling TSV files)
Example
with open('data.tsv', 'r') as file:
reader = csv.reader(file, dialect='tsv')
for row in reader:
print(row)