This article is a follow-up to Reading 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
Photo by Andrew Furlan / Unsplash
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 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.
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.[1]
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:
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/
Top comments (0)