AppDividend
Latest Code Tutorials

Python CSV Tutorial | Reading and Writing CSV Files in Python

0

Python CSV Tutorial | Reading and Writing CSV Files in Python is today’s topic. 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.

Content Overview

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 Python’s Built-in CSV Library

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

The csv library contains the objects and other code to read, write, and process data from and to the CSV files.

Python CSV module includes all the necessary functions built in. They 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 Tutorial

Let’s read and write the CSV files using 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 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 app.py and add the following code inside that file.

# app.py

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
        else:
            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 is 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:

  • The delimiter specifies the character used to separate each field. The default is the comma (‘, ‘).
  • The quotechar specifies the unique character used to surround fields that contain the delimiter character. The default is the double quote (‘ ” ‘).
  • 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.

# app.py

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:

  • 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.
  • If quoting is set to the csv.QUOTE_ALL, then .writerow() will quote all the fields.
  • 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.
  • 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, Python CSV Tutorial | Reading and Writing CSV Files in Python is over.

Leave A Reply

Your email address will not be published.

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