Python Manipulating CSV Files

Manipulating CSV Files with Python

Manipulating CSV Files with Python

CSV (Comma-Separated Values) is a common data exchange format that uses commas to separate fields and newlines to separate rows. In Python, we can easily use the csv module to read, write, and manipulate CSV files.

Reading CSV Files

Let’s first look at how to read a CSV file. Suppose we have a file named data.csv with the following content:

Name, Age, Gender
Alice, 25, Female
Bob, 30, Male
Carol, 28, Female

<p>We can use the <code>reader function in the csv module to read this file and iterate through each row of data:

import csv

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

Running the above code produces the following output:

['Name', 'Age', 'Gender']
['Alice', '25', 'Female']
['Bob', '30', 'Male']
['Carol', '28', 'Female']

As we can see, each row of data has been converted into a list, making it easier to process.

Writing to a CSV File

Next, let's see how to write data to a CSV file. Suppose we have a dictionary list data with the following contents:

data = [
{'Name': 'David', 'Age': 35, 'Gender': 'Male'},
{'Name': 'Eve', 'Age': 32, 'Gender': 'Female'}
]

We can use the writer function in the csv module to write the data to a new CSV file output.csv:

import csv

data = [
{'Name': 'David', 'Age': 35, 'Gender': 'Male'},
{'Name': 'Eve', 'Age': 32, 'Gender': 'Female'}
]

fields = ['Name', 'Age', 'Gender']

with open('output.csv', 'w') as file:
writer = csv.DictWriter(file, fieldnames=fields)

writer.writeheader()
for row in data:
writer.writerow(row)

Running the above code will produce a new output.csv file with the following content:

Name, Age, Gender
David, 35, Male
Eve, 32, Female

Manipulating CSV Files

In addition to reading and writing CSV files, the csv module provides several other functions, such as reading the first row of a CSV file as a header and specifying field delimiters. Here are some common example codes:

Reading the first row of a CSV file as a header

Suppose we have a CSV file data.csv with the following content:

Id|Name|Age
1|Alice|25
2|Bob|30
3|Carol|28

We can specify a delimiter to read this file and use the first row as the header:

import csv

with open('data.csv', 'r') as file:
reader = csv.DictReader(file, delimiter='|')

for row in reader:
print(row)

Running the above code produces the following output:

{'Id': '1', 'Name': 'Alice', 'Age': '25'}
{'Id': '2', 'Name': 'Bob', 'Age': '30'}
{'Id': '3', 'Name': 'Carol', 'Age': '28'}

As you can see, we've successfully specified the delimiter and used the first row as the table header.

Ignore Blank Lines When Writing CSV Files

Sometimes, we need to ignore blank lines when writing to CSV files. We can use the extrasaction='ignore' parameter to achieve this:

import csv

data = [
{'Name': 'David', 'Age': 35, 'Gender': 'Male'},
{'Name': '', 'Age': '', 'Gender': ''}
]

fields = ['Name', 'Age', 'Gender']

with open('output.csv', 'w') as file:
writer = csv.DictWriter(file, fieldnames=fields, extrasaction='ignore')

writer.writeheader()
for row in data:
writer.writerow(row)

Running the above code, the output.csv file will contain only one row of data, David,35,Male. Blank rows will be ignored.

Summary

The csv module allows us to conveniently read, write, and manipulate CSV files. In addition to the functions described above, the csv module provides many more methods and parameters. You can learn more about its functionality and usage by consulting the official documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *