Add List to CSV File Using Python 3

      No Comments on Add List to CSV File Using Python 3

This is a simple example on converting List data to a standard Comma Separated (CSV) file using Python 3. We are using the Python Standard library and the CSV module to create our CSV file.
This module is loaded with options so we are just including the more often used to get the job done.

Python Lists

The CSV module needs to be imported from the python standard library. In this example we create 2 lists for some of the States and their capital cities. The first list is called “Header” and we will be adding it as the Head Row for the file,
The header is not really necessary and we could just simply add the States list without a header row a t all.

import csv

Header = ['State Name','Abbrev','Capital']
States = [
['Alabama','AL','Montgomery'],
['Alaska','AK','Juneau'],
['Arizona','AZ','Phoenix'],
['Arkansas','AR','Little Rock'],
['California','CA','Sacramento'],
['Colorado','CO','Denver'],
['Connecticut','CT','Hartford'],
['Delaware','DE','Dover'],
['Florida','FL','Tallahassee'],
['Georgia','GA','Atlanta'] ]

We are using the python “with statement” to create and populate the file as it offers better exception handling than “try” and “finally”. We first open the file for writing:

with open('states.csv', 'w') as s:

Then we define the CSV writer and write the header and States accordingly. Notice the methods are singular and plural:

    write = csv.writer(s)
    write.writerow(Header) ## write the header
    write.writerows(States) ## write the data

Write Line by Line | One Row

With the file open we can use the writer to continue to write a single row line by line in the csv file. Or just append another List to the end of the file.

    write.writerow(['Super State','SS','AwesomeNess'])## Append to the existing data

    write.writerows(States2) ## write 2nd list of data

Change the Delimiter

You can also specify a different delimiter than the comma that is the default. As an example this assignment will use the semicolon instead of the default comma.

    write = csv.writer(s, delimiter=';',)  

Quotes and No Quotes

Additionally the fields can be encapsulated in quotes as to avoid escape character mismatch and datatype translation.

write = csv.writer(s, quoting=csv.QUOTE_ALL) 

Complete Example Code

import csv

Header = ['State Name','Abbrev','Capital']
States = [
['Alabama','AL','Montgomery'],
['Alaska','AK','Juneau'],
['Arizona','AZ','Phoenix'],
['Arkansas','AR','Little Rock'],
['California','CA','Sacramento'],
['Colorado','CO','Denver'],
['Connecticut','CT','Hartford'],
['Delaware','DE','Dover'],
['Florida','FL','Tallahassee'],
['Georgia','GA','Atlanta'] ]

SingleColumn = ['United States','Canada','Mexico','Brazil']

with open('states.csv', 'w') as s:
   ## write = csv.writer(s, dialect='excel', delimiter=',',)  
   ## write = csv.writer(s, quoting=csv.QUOTE_ALL) 
    write = csv.writer(s)
    write.writerow(Header) ## write the header
    write.writerows(States) ## write the data
    write.writerow(['Super State','SS','AwesomeNess'])## Append to the existing file
    write.writerows(States) ## write data a second time