The Python CSV Module Read and Write Methods in Action

pythoncsvThe advantages of Python as a programming language

Python is a general, all-purpose programming language. The language was designed and written with readability in mind. This means that Python Syntax and code was designed to be as simple as possible. Python therefore allows programmers to create applications using fewer lines of code than programming languages like C, for example.

Python offers various programming styles including object oriented programming, imperative programming, functional programming and procedural programming. This means that the transition to Python from other programming languages can be quite easy for programmers who are used to programming in one of these styles.

Python is often used as a scripting language but there are modules that allow Python programmers to create stand-alone applications too. Python is therefore a very versatile programming language.

Because of the simple scripting ideology that Python was based on, Python is actually an ideal language for beginners who are new to programming and coding. Python’s easily readable code means that beginners can create their own applications fairly quickly and easily. Python also has an interactive mode that allows testing of code snippets so beginners can gain confidence in their code as well as testing their code as they go along. Python is also free to download and use, so no initial outlay is required to start obtaining the skills you need to secure a lucrative job as a web developer or programmer.

If you are interested in learning to code in Python or if you are a web developer who is looking to increase your coding skills then Python for Beginners offers a great introduction to programming in Python.

Modules available for Python

Python offers a comprehensive library of modules that can be used in your python programs. Modules are essentially snippets of code that are written to perform certain predefined functions. Modules save you the time of writing your own code snippets. Python allows you to create your own modules too.

Some of the most popular modules in Python include data representation modules like the array module, core modules like the various language support modules and file format modules like the htmllib parser that works with the format module to allow for python to render output in html format.

This tutorial will be focused on the use of the CSV module. The CSV module allows you to read from CSV files and also allows you to write to them. To understand the tutorial and follow the concepts, it is highly recommended that you have a basic understanding of Python. A course like Python for Rookies will give you the skills you need to understand the basics of Python used in this tutorial.

CSV files and working with CSV files

CSV is an acronym for Comma Separated Values. The CSV format is a very common data format used by various programs and applications as a format to export or import data. Most spreadsheet and database applications offer export and import facilities for CSV files.

CSV files generally store tables of data that contain fields stored within records. Each field in the record is separated by a character within the CSV file and generally new records start on a new line.  This is what a CSV file looks like that has been exported from Excel:

Although CSV is a very common import/export file format, there is no standard format with regards to the characters used for the separation of the fields. That can make creating code to import and export data from a CSV file tricky to write. But the CSV module available for Python has taken that fact into account and as you will see later, the Python CSV module allows you to use routines that will help you determine the format of the CSV you need to access.

How to use the CSV module in Python to open a CSV File

Python is an extremely flexible language where it comes to modules. You can write your own modules for your programs or rely on the hundreds of existing modules to create your code. Whether you have written your own module or whether you want to use a predefined module, you need to import the module into your code to use it.

To import a module into your code is very simple because of the simplistic Python syntax. To import the CSV module, you merely need to use the “import” syntax at the beginning of your code. For this tutorial we will use the file above and call it tutorial.csv.

The CSV module allows you to read data from tutorial.csv, to write data to tutorial.csv, to determine the format of tutorial.csv and to create a dialect for tutorial.csv.

To import the CSV module to have access to the functions predefined in the module, you would add the following line to the beginning of your code:

How to read data within the CSV file

The Python CSV module contains a number of objects that allow you to manipulate or create data within a CSV file. The reader object allows you to open and read the contents of a CSV within Python. The syntax to open and read a CSV file in python is:

The reader object is initialized using the csv.reader instruction. The object contains the following parameters: the csvfile parameter, the dialect parameter and the fmtparams. The csvfile tells the object what file to open. The dialect parameter is optional and can be used to specify the format of the CSV file, and the fmtparams can be used to specify specific formatting parameters.

Before we can use the Python CSV module to read our file we need to open the file in Python. To open the file we use the Open command. The syntax for the Open command is:

The filename is the filename of the file to be opened. The mode refers to how the file will be used once it’s opened. Modes include the read mode, write mode, append mode. To read our file we will open the file in binary read mode or ‘rb’ for now.

Here is the simplest version of code to open our tutorial.csv file:

The above code imports the module. It then opens the tutorial CSV file and reads the records in the file into an object we created called reader. Then for each row in the reader object, it outputs the data.

The reader object has the following public methods associated with the object. The csvreader.next() method reads the next record from the csv file. The csvreader.dialect method is a description of the dialect used by the parser that is read only format. The csvreader.line_num method returns the number of lines in the csv file. Take note that depending on the length of the records, records may span more than one line so the csvreader.line_num may not equal the number of records within the file.

Working with data from the CSV file

Once you have opened the CSV file and read the data into your object you can work with or manipulate the data within the object. The following code will read the records into our object and then assign row 0 of the data to a header column. We will then use the header columns to output the data in a format that makes sense using the IF statement to assign the header row and the FOR statement to iterate through the records.

For more advanced Python programming and tutorials on how to use the IF and FOR statements in Python, The Ultimate Python Programming Tutorial will teach you about the basic data types within Python as well as methods to use; the IF statements and various programming loops available in Python.

Here is the code that will open our CSV file, create the headers without our object and read the data within the object.

The output of the above code would look like this for each record. (We have displayed the first record only):

How to write data to the CSV file

Writing to a CSV file is just as simple as reading from a CSV file. The CSV module in Python provides a write object that allows you to write and save data in CSV format.

The syntax and parameters for the write object are very similar to the syntax for the read object. The syntax for the write object can be expressed as follows:

The writer function of the object is initialized with the csv.writer statement. The csvfile parameter is used to indicate the name of the CSV file that needs to be written into. The dialect parameter is optional but can be used to specify a format for the CSV file output. The fmtparams is also an optional parameter that allows you to specify the format of the data to be written.

Here is the code you would need to write our data to a file saved in CSV format, including code comments:

# import the module.

import csv

# open a file for writing.

tutorial_out = open(‘tutorial.csv’, ‘wb’)

# create the csv writer object.

mywriter = csv.writer(csv_out)

# create an object called data that holds the records.

data = [

(Employee No;Employee Name;Job Desription;Salary),

(123453;Jack;CEO;12000),

(453124;Jane;Director;25000),

(4568354;Sally;Marketing;68000),

(684535;Harry;Sales;56000)

]

# write the rows.

for item in data:

    mywriter.writerow(item)

# always make sure that you close the file.

# to ensure the data is saved.

tutorial_out.close()

The csv writer object has a number of public methods available to it. The csvwriter.writerow(row) method, writes a single row to the object using the default dialect or dialect selected. The csvwriter.writerows(rows) writes all of the rows to the file object using the default or selected dialect. The csvwriter.dialect refers to the dialect or format of the CSV file to be written to.

How to use the dialect function within the CSV module

As mentioned above, the CSV format is a very popular file format for data storage. There are however no official standards for the CSV file format itself and the characters used to separate the field of the cells can range from commas to semi-colons to tab characters.

The dialect method allows you, as a programmer, to specify the format for the CSV file for reading or writing purposes. The dialect class is specified within the reader or writer object. To specify the output format of the CSV file using the tab character as the value that defines the separator between fields, you can set the value of delimiter parameter within the writer object to ‘/t’.

# import the module.

import csv

# open a file for writing.

tutorial_out = open(‘tutorial.csv’, ‘wb’)

# create the csv writer object.

mywriter = csv.writer(csv_out, delimiter=’/t’)

The CSV file will look like this:

The dialect function in Python includes a standard formatting function for formatting CSV files in Excel format. To output a file in Excel CSV format the parameter must include the dialect=’excel’ parameter. The code would look like this:

# import the module.

import csv

# open a file for writing.

tutorial_out = open(‘tutorial.csv’, ‘wb’)

# create the csv writer object.

mywriter = csv.writer(csv_out, dialect=’excel’)

How to use the Sniffer function within the CSV module

There are times when you will need to make provision for reading CSV files that are of unknown origin and you may therefore not know the format of the CSV file. The CSV module allows for the determination of the format using the Sniffer class. The Sniffer class provides a way of determining the format of the CSV file.

Take a look at the following code:

# import the module.

import csv

# use the sniffer class to determine the format of the CSV file before

# reading the contents

with open(‘tutorial.csv’, ‘rb’) as csvfile:

               dialect = csv.Sniffer().sniff(csvfile.read(1024))

               csvfile.seek(0)

               reader = csv.reader(csvfile, dialect)

Python is an extremely powerful language for scripting websites that need to include the manipulation of CSV files. For more advanced courses and tutorials on Python, why not sign up for Web programming with python and start developing your Python skills today?