Writing Python Dictionary to CSV

Writing a Python Dictionary to CSV

CSV (Comma Separated Values) is a common file format, often used to store tabular data. Python provides many libraries for manipulating CSV files, the most commonly used of which is the csv module.

In real-world work, we often need to write data from a Python dictionary to a CSV file for backup and sharing. This article will explain how to write dictionary data to a CSV file using Python for your reference.

Creating Dictionary Data

In this article, we’ll use the following dictionary data as an example:

data = [
{'name': 'Alice', 'age': 25, 'gender': 'female'},
{'name': 'Bob', 'age': 30, 'gender': 'male'},
{'name': 'Charlie', 'age': 35, 'gender': 'male'}
]

The above dictionary data contains the names, ages, and genders of three people. Next, we’ll use Python to write this dictionary data to a CSV file.

Creating a CSV File

Before we begin writing data, we need to create a CSV file and write the data to it. The following code creates a file:

import csv

filename = ‘data.csv’
with open(filename, ‘w’, newline=”) as file:
writer = csv.writer(file)
writer.writerow([‘name’, ‘age’, ‘gender’])

In the above code, we create a file-writing object using the csv.writer object and write the header row using the writerow method. We specify the file name (‘data.csv’) and the write mode (‘w’). We also use the newline=” parameter to avoid writing blank lines.

Next, we use the csv.writer object to write the dictionary data to the file. Here’s the code to write the data:

import csv

data = [
{‘name’: ‘Alice’, ‘age’: 25, ‘gender’: ‘female’},
{‘name’: ‘Bob’, ‘age’: 30, ‘gender’: ‘male’},
{‘name’: ‘Charlie’, ‘age’: 35, ‘gender’: ‘male’}
]

filename = ‘data.csv’
with open(filename, ‘w’, newline=”) as file:
writer = csv.writer(file)
writer.writerow([‘name’, ‘age’, ‘gender’])
for row in data:
writer.writerow(row.values())

In the code above, we use for We loop through the dictionary data and write each value to a CSV file. We use the row.values() method to retrieve each value.

Reading a CSV File

After writing the data to a CSV file, we can read it using the following code:

import csv

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

In the above code, we use a csv.DictReader object to read the CSV file and convert it into a dictionary object. We use a for loop to iterate over the object and print out each row of data. The output is as follows:

{'name': 'Alice', 'age': '25', 'gender': 'female'}
{'name': 'Bob', 'age': '30', 'gender': 'male'}
{'name': 'Charlie', 'age': '35', 'gender': 'male'}

In the above output, we can see that each row of data in the CSV file has been converted into a Python dictionary object.

Conclusion

In this article, we introduced how to use Python to write dictionary data to a CSV file and how to read a CSV file and convert it into a Python dictionary object. CSV files are a commonly used data format and are very helpful for backing up and sharing data. I hope this article is helpful.

Leave a Reply

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