Home » Programming » Python » Python Write Generate Csv

How to Generate & Write CSV Files in Python, With Examples

This article will show you how to generate CSV (Comma Separated Values) data, and write it to a file, with examples.

What is CSV (Comma Separated Values)

CSV (Comma Separated Values) is a format for storing and transmitting data, usually in a text file, in which the individual values are separated by a comma (,). Each row in a CSV file represents an individual record containing multiple comma separated values.

A CSV file looks like this:

name,age,favourite colour
Fred,54,green
Mary,31,pink
Steve,12,orange

Above, the CSV describes three people, with information about their nameage, and favourite colour. Each row contains a record for each person, with the values separated by commas. The first row in a CSV file usually contains the header, which describes the purpose of the values.

The order of the values in each record is important, as it’s the position of each value that denotes what it is — matching a header in the same position.

Basically, think of it like a spreadsheet, where the columns are separated by commas, and the rows are separated by newlines.

Generating CSV Data Manually

As CSV data is just text, it’s really easy to generate – just generate some text with commas and newlines separating the values and records!

myCSV = "Sandra,26,red\n"

Above, a single CSV record is defined as a Python string variable. Note the comma separation, and the use of \n to create a new line at the end of the string Multiple records can be created by appending strings:

myCSV = "Sandra,26,red\n"
myCSV = myCSV + "Tim,19,yellow\n"

Generate and Wite CSV Files Using the csv Python Library

The above is a bit tedious, and would quickly become hard to manage when dealing with lots of data. Python includes the csv library specifically for generating, saving, and reading CSV data and files.

import csv

peopleData = [
    ["name", "age", "favourite colour"],
    ["Fred", 54, "green"],
    ["Mary", 31, "pink"],
    ["Steve", 12, "orange"],
    ["Sandra", 26, "red"],
    ["Tim", 19, "yellow"]
]

with open('people.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(peopleData)

Above, the list peopleData is defined. Each item in the list is another list, containing the values which should be added to each record in the CSV file. The writer object from the csv library is then used to convert peopleData to CSV and write it to a text file.

The resulting people.csv file will look like this:

name,age,favourite colour
Fred,54,green
Mary,31,pink
Steve,12,orange
Sandra,26,red
Tim,19,yellow

Ready to be opened in your favourite spreadsheet editor.

This is much easier than trying to generate and write the CSV data manually.

Delimiters and Quotes

As commas are used to delimit data in the CSV format, using commas in the values themselves presents a bit of a problem. This is solved by wrapping values containing commas in quotes:

name,age,favourite colour
"Fred, formerly known as Freddy",54,green

By default, the csv library in Python will only add quotes to values containing the delimiter. You can force it to quote all non-numeric values by adding the quoting=csv.QUOTE_NONNUMERIC option when initializing the csv.writer object:

import csv

peopleData = [
    ["name", "age", "favourite colour"],
    ["Fred", 54, "green"],
    ["Mary", 31, "pink"],
]

with open('people.csv', 'w', newline='') as file:
    writer = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC, delimiter=';')
    writer.writerows(peopleData)

It’s also possible to use a different delimiter altogether. Below, instead of a comma, a semicolon is used to separate the values:

import csv

peopleData = [
    ["name", "age", "favourite colour"],
    ["Fred", 54, "green"],
]

with open('people.csv', 'w', newline='') as file:
    writer = csv.writer(file, delimiter=';')
    writer.writerows(peopleData)

Note that, even with a different delimiter, it’s still usually called a Comma Separated Values file.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment