Easy Exporting CSV Data with  Python
Tags: python, csv, scripting, output
Published: 2017-10-11 21:15:11

---
title: "Easy Exporting CSV Data with  Python"
published: true
tags: python,csv,scripting,output
canonical_url: http://scriptingwithpython.com/easy-exporting-data-as-a-csv-in-python/
---

 ![Easy Exporting CSV Data with Python](http://scriptingwithpython.com/content/images/2017/10/5aa2510fc2dc493a66983ce82d63cce1.svg)

This article is a follow-up to [Reading CSV Data with Python](http://scriptingwithpython.com/processing-csv-data-with-python).

In that article, I described what CSV files are and how to read CSV Data. The flip side to reading data is writing data out to a file. I will summarize again what a CSV file is then go into how to write data in this format.

CSV files are extremely common. I run into them all the time at work when transferring data. At home, a CSV file is almost always an option when exporting a file from the bank or out of Google Sheets.

# What is CSV?

**CSV** = **C** omma **S** eparated **V** alues

![Easy Exporting CSV Data with Python](https://images.unsplash.com/photo-1485167729757-d0c4103392dc?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=ab2f7cc21dba01f8e8cb96132d63c7a6)  
<small>Photo by <a href="https://unsplash.com/@andrewfurlan?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Andrew Furlan</a> / <a href="https://unsplash.com/?utm_source=ghost&amp;utm_medium=referral&amp;utm_campaign=api-credit">Unsplash</a></small>

We see them all the time but there seems to be some confusion about what a CSV file really is. When I talk to people about CSV files, I realize that most of us equate them with Microsoft Excel documents. This is probably because Excel is often the default program for opening CSV files.

Here is the thing. A CSV file is simply a text file. That's it, nothing more. The file extension is usually `.csv` but it doesn't have to be. Any file extension is simply a hint to what is contained in the file and doesn't actually determine or control the content type. The [Wikipedia Article](https://en.wikipedia.org/wiki/Comma-separated_values) about CSV files states that:

> "CSV" is not a single, well-defined format.  
> As a result, in practice the term "CSV" might refer to any file that:
> 
> - is plain text using a character set such as ASCII, various Unicode character sets
> - consists of records (typically one record per line), with the records divided into fields separated by delimiters
> - where every record has the same sequence of fields

Here is an example. I have a file called `users.csv` sitting on my desktop. In a spreadsheet program (Excel, Google Sheets, LibreOffice, Numbers, etc) this file would look like this.

![Easy Exporting CSV Data with Python](http://scriptingwithpython.com/content/images/2017/10/csv_file_1.1.png)

Those columns and rows are nice and fancy. We think there is something magical going one but it isn't. The contents of this file are really just:

```
user_id,first_name,last_name,email
234,Kevin,Crommer,example@example.com
235,Carl,Marx,cmarx@example.com
```

You can open a CSV file with a plaintext editor to see the true contents. It is very similar to viewing the source of a web page and looking at the underlying HTML.

A spreadsheet programs reads the CSV file and displays to the user (you and me) a pretty version that makes it easy to see the columns and rows.

As you can see in my example, A CSV file typically has one row per record. In this case there are 4 columns. The headers of the columns are `user_id`, `first_name`, `last_name` and `email`. Records will have one or more columns that are usually, not always, separated by a comma.

## Enough Review Already, Get to the Writing Data Good Stuff

Ok, ok. Let's learn about how to write CSV data to a file or file-like object.

There are a few essentials points that you will need to remember when writing out CSV data.

### Do not try to write CSV without a library.

You have better things to do with your time than to figure out how to handle every possible CSV variation. Believe me, I speak from experience. I will admit to wasting days of time before I discovered the built-in (yes built-in) `csv` module. Your efforts are better spent getting stuff done.

### The `csv` module is Awesome

The CSV Module is built into every modern version of Python.<sup><a href="http://scriptingwithpython.com/easy-exporting-data-as-a-csv-in-python/#fn1" id="fnref1">[1]</a></sup>

This module handles all of the intricacies of reading and writing CSV data. It is crazy fast and just works.

To quote the [Python Docs](https://docs.python.org/3/library/csv.html):

> The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV formats.
### Enough Enough already... Get to An Example

Okay, okay. Let's start with an example.

Finish reading this article at http://scriptingwithpython.com/easy-exporting-data-as-a-csv-in-python/