Python CSV: Reading and Writing CSV Files in Python

Python CSV Read and Write Files Tutorial With Example

Python CSV library provides the functionality to read and write CSV files. It is specifically designed to work out of the box with Excel-generated CSV files; it is effortless to work with a variety of CSV formats.

Exchanging information through text files is the standard way to share info between programs and one of the most popular formats for transferring data in the CSV format.

You don’t need to build your own CSV parser from scratch. There are acceptable standard libraries you can use.

In this post, you will learn how to read, process, and parse the CSV files using Python. You will see how CSV files work, learn the all-important CSV library built into Python.

What is a CSV File?

CSV(Comma Separated Values) files are used to store a large number of variables or data. They are incredibly simplified spreadsheets think Excel, only the content is stored in plaintext. Python CSV module is a built-in function that allows Python to parse these types of files.

The text inside the CSV file is laid out in rows, and each of those has columns, all separated by commas.

Technically in CSV files, the first row is column names in SQL tables, and then the other rows are the data according to the columns.

Every line in a CSV file is a row in the spreadsheet, while the commas are used to define and separate cells.

The structure of the CSV file is following.

column 1 name,column 2 name, column 3 name
first row data 1,first row data 2,first row data 3
second row data 1,second row data 2,second row data 3
third row data 1,third row data 2,third row data 3
fourth row data 1,fourth row data 2,fourth row data 3

Parsing CSV Files with inbuilt CSV Library

Python CSV module includes all the necessary functions.

The CSV function list is following.

  1. csv.reader()
  2. csv.writer()
  3. csv.register_dialect()
  4. csv.unregister_dialect()
  5. csv.get_dialect()
  6. csv.list_dialects()
  7. csv.field_size_limit

Python CSV Example

Python CSV module contains the objects and other code to read, write, and process data from and to the CSV files.

Let’s read and write the CSV files using the Python CSV module.

Reading CSV Files

If we need to pull the data from the CSV file, you must use the reader function to generate the reader object.

The reader function is formed to take each line of the file and make the list of all columns. Then, you choose the column you want a variable data for.

The CSV file is opened as the text file with Python’s built-in open() function, which returns the file object. This is then passed to the reader, which does the heavy lifting.

The syntax for reading a CSV file in Python is the following.

import CSV
With open(‘some.csv’, ‘rb’) as f:
reader = csv.reader(f)
for row in reader:
print row

Let’s see the following example.

Let’s create a CSV file called data.csv and add the following content.

column 1 name,column 2 name, column 3 name
first row data 1,first row data 2,first row data 3
second row data 1,second row data 2,second row data 3
third row data 1,third row data 2,third row data 3
fourth row data 1,fourth row data 2,fourth row data 3

In the same directory, create a file called and add the following code inside that file.


import csv

with open('data.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
            print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1
    print(f'Processed {line_count} lines.')

Notice the first line of code is used to import the CSV module.

Then we have used the open() function and pass the file which we need to read and then use the CSV module’s reader function to create an object and iterate that object to get the row and columns.

Each row returned by the reader is the list of String elements containing the data found by removing the delimiters. 

The first row returned contains the column names, which are handled in a special way.

See the output below.

Python CSV Tutorial | Reading and Writing CSV Files in Python

See in the output; we have differentiated the columns names and rows data.

Optional Python CSV reader Parameters

The reader object can handle the different styles of CSV files by specifying additional parameters, some of which are shown below:

  1. The delimiter specifies the character used to separate each field. The default is the comma (‘, ‘).
  2. The quotechar specifies the unique character used to surround fields that contain the delimiter character. The default is the double quote (‘ ” ‘).
  3. The escapechar specifies the character used to escape the delimiter character, in case quotes aren’t used. The default is no escape character.

Writing to CSV Files

You can also write to the CSV file using the writer object and the .write.row() method.

If the writing file has not been created, then it will be automatically created. Let’s see the following example.


import csv

with open('student.csv', mode='w') as student_file:
    student_writer = csv.writer(student_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    student_writer.writerow(['Krunal Lathiya', 'GTU', 'BE'])
    student_writer.writerow(['Ankit Lathiya', 'GTU', 'MCA'])

    print('Writing has been completed')

In the above code, we have not created the student.csv file, still created a writer object and written the two rows.

Run the file and see the output.

Writing to CSV Files in Python

You can see the student.csv file inside your project directory.

Krunal Lathiya,GTU,BE
Ankit Lathiya,GTU,MCA

The quotechar is an optional parameter that tells the writer object which characters to use to quote fields when writing the file. 

Whether quoting is used or not, however, is determined by an optional quoting parameter:

  1. If quoting is set to the csv.QUOTE_MINIMAL, then .writerow() will quote fields only if they contain a delimiter or a quotechar. This is by default case.
  2. If quoting is set to the csv.QUOTE_ALL, then .writerow() will quote all the fields.
  3. If quoting is set to the csv.QUOTE_NONNUMERIC, then .writerow() will quote all the fields containing the text data and convert all numeric fields to a float data type.
  4. If quoting is set to the csv.QUOTE_NONE, then .writerow() will escape delimiters instead of quoting them. In that case, you also must provide the value for the escapechar optional parameter.

Finally, the Python CSV Example is over.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.