excel

How to Read and Write CSV Files in Python

The CSV format is the most commonly used import and export format for databases and spreadsheets. This tutorial will give a detailed introduction to CSV’s and the modules and classes available for reading and writing data to CSV files. It will also cover a working example to show you how to read and write data to a CSV file in Python.

What Is a CSV File?

A CSV (comma separated values) file allows data to be saved in a tabular structure with a .csv extension. CSV files have been used extensively in e-commerce applications because they are considered very easy to process. Some of the areas where they have been used include:

  • importing and exporting customer data
  • importing and exporting products
  • exporting orders
  • exporting e-commerce analytic reports

Reader and Writer Modules

The CSV module has several functions and classes available for reading and writing CSVs, and they include:

  • csv.reader function
  • csv.writer function
  • csv.Dictwriter class
  • csv.DictReader class

csv.reader

The csv.reader module takes the following parameters:

  • csvfile: This is usually an object which supports the iterator protocol and usually returns a string each time its __next__() method is called.
  • dialect='excel': An optional parameter used to define a set of parameters specific to a particular CSV dialect.
  • fmtparams: An optional parameter that can be used to override existing formatting parameters.

Here is an example of how to use the csv.reader module.

csv.writer module

This module is similar to the csv.reader module and is used to write data to a CSV. It takes three parameters:

  • csvfile: This can be any object with a write() method.
  • dialect='excel': An optional parameter used to define a set of parameters specific to a particular CSV.
  • fmtparam: An optional parameter that can be used to override existing formatting parameters.

DictReader and DictWriter Classes

The DictReader and DictWriter are classes available in Python for reading and writing to CSV. Although they are similar to the reader and writer functions, these classes use dictionary objects to read and write to csv files.

DictReader

It creates an object which maps the information read into a dictionary whose keys are given by the fieldnames parameter. This parameter is optional, but when not specified in the file, the first row data becomes the keys of the dictionary.

Example:

DictWriter

This class is similar to the DictWriter class and does the opposite, which is writing data to a CSV file. The class is defined as csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)

The fieldnames parameter defines the sequence of keys that identify the order in which values in the dictionary are written to the CSV file. Unlike the DictReader, this key is not optional and must be defined in order to avoid errors when writing to a CSV.

Dialects and Formatting

A dialect is a helper class used to define the parameters for a specific reader or writer instance. Dialects and formatting parameters need to be declared when performing a reader or writer function.

There are several attributes which are supported by a dialect:

  • delimiter: A string used to separate fields. It defaults to ','.
  • double quote: Controls how instances of quotechar appearing inside a field should be quoted. Can be True or False.
  • escapechar: A string used by the writer to escape the delimiter if quoting is set to QUOTE_NONE.
  • lineterminator: A string used to terminate lines produced by the writer. It defaults to 'rn'.
  • quotechar: A string used to quote fields containing special characters. It defaults to '"'.
  • skipinitialspace: If set to True, any white space immediately following the delimiter is ignored.
  • strict: If set to True, it raises an exception Error on bad CSV input.
  • quoting: Controls when quotes should be generated when reading or writing to a CSV.

Reading a CSV File

Let’s see how to read a CSV file using the helper modules we have discussed above.

Create your CSV file and save it as example.csv. Ensure that it has the .csv extension and fill in some data. Here we have our CSV file which contains the names of students and their grades.

Creating a spreadsheet to generate a CSV

Below is the code for reading the data in our CSV using both the csv.reader function and the csv.DictReader class.

Reading a CSV File With csv.reader

In the code above, we import the CSV module and then open our CSV file as File. We then define the reader object and use the csv.reader method to extract the data into the object. We then iterate over the reader object and retrieve each row of our data.

We show the read data by printing its contents to the console. We have also specified the required parameters such as delimiter, quotechar, and quoting.

Output

Reading a CSV File With DictReader

As we mentioned above, DictWriter allows us to read a CSV file by mapping the data to a dictionary instead of strings as in the case of the csv.reader module. Although the fieldname is an optional parameter, it’s important to always have your columns labelled for readability.

Here’s how to read a CSV using the DictWriter class.

We first import the csv module and initialize an empty list results which we will use to store the data retrieved. We then define the reader object and use the csv.DictReader method to extract the data into the object. We then iterate over the reader object and retrieve each row of our data.

Finally, we append each row to the results list and print the contents to the console.

Output

As you can see above, using the DictReader class is better because it gives out our data in a dictionary format which is easier to work with.

Writing to a CSV File

Let’s now see how to go about writing data into a CSV file using the csv.writer function and the csv.Dictwriter class discussed at the beginning of this tutorial.

Writing to a CSV File Using csv.writer

The code below writes the data defined to the example2.csv file.

First we import the csv module, and the writer() function will create an object suitable for writing. To iterate the data over the rows, we will need to use the writerows() function.

Here is our CSV with the data we have written to it.

Our CSV with the data we have written to it

Writing to a CSV File Using DictWriter

Let’s write the following data to a CSV.

The code is as shown below.

We first define the fieldnames, which will represent the headings of each column in the CSV file. The writerrow() method will write to one row at a time. If you want to write all the data at once, you will use the writerrows() method.

Here is how to write to all the rows at once.

Conclusion

This tutorial has covered most of what is required to be able to successfully read and write to a CSV file using the different functions and classes provided by Python. CSV files have been widely used in software applications because they are easy to read and manage and their small size makes them relatively fast to process and transfer.

Don’t hesitate to see what we have available for sale and for study in the marketplace, and don’t hesitate to ask any questions and provide your valuable feedback using the feed below.

Powered by WPeMatico

Leave a Comment

Scroll to Top