Working with CSV files in Python : Part I

Words
576
Reading
3 min
Listen
Play
9y

What Will I Learn?

In the previous tutorial, we learnt to read and write files. CSV files are different. This tutorial covers following topics.

  • CSV files and CSV modules
  • Read and write CSV files
  • Read and write CSV files by mapping datas to dictionaries

Requirements

  • A PC/laptop with any Operating system such as Linux, Mac OSX, Windows OS
  • Preinstalled Python
  • Preinstalled Code Editors such as Atom, Sublime text or Pycharm IDE

Note: This tutorial is performed in Pycharm IDE in laptop with Ubuntu 17.1, 64 bit OS.

Difficulty

Intermediate, anyone with basic programming knowledge of python or any other programming language can catch up this tutorial. I recommend learning basics of python programming before starting this tutorial. The links for previous tutorials are at the bottom of this tutorial.

Tutorial Contents

What are CSV files?

CSV stands for Comma(,) Separated Values. CSV files are the files which contain data separated by commas in the tabular structure. It can be viewed in tabular form in any spreadsheet softwares such as Microsoft Excel, Open Office Calc, Google Spreadsheets, Libre Office Calc, etc. It has dot csv(.csv) extension.

CSV module:

Python CSV module allows programmers to read and write CSV files. To use CSV module we need to import it at the begining of the program.

import csv

It has many inbuilt methods to perform the operation on CSV files. Some of them are as follows:

  • csv.writer(csvfilename,dialect) allows us to write in CSV files and returns writer objects. csvfilename is the name of file which should be wrote.Dialects contains specific formatting parameters such as delimeter, quotechar, etc.
#importing CSV module 
import csv
# Creating  or opening csv file with write mode
with open('newcsvfile.csv', 'w') as csvfile:
    # Calling inbuilt write method of csv module 
    csv_writer = csv.writer(csvfile, delimiter=',')
   # Calling writerow method to iterate over passed values
    csv_writer.writerow([ 'Name', 'Address', 'Roll' ])
    csv_writer.writerow([ 'Programming', 'Hub', '1' ])
    csv_writer.writerow([ 'Python', 'Program', '2' ])
    csv_writer.writerow([ 'Open', 'Source', '3' ])

In above code, at first we have imported CSV module, then we opened the csv file in write mode. Comma delimeter is passed to separate each values in a line to separate it with comma.
csvwriter.writerow() allows us to write row parameters of writer object returned from csvwriter() which formats the parameters passed to it with given diallects.

After running above program, output file produced is found here.

If we open it in spreadsheet softwares then it is like:

  • csv.reader(csvfilename,dialect) allows to read csv files and returns reader objects.
import csv
with open('newcsvfile.csv', 'r') as csvfile:
    csv_reader = csv.reader(csvfile)
    for line in csv_reader:
        print(line)

We called csv.reader method to read the csv file which we created earlier and ran the loop to print each line.

Output:

['Name', 'Address', 'Roll']
['Programming', 'Hub', '1']
['Python', 'Program', '2']
['Open', 'Source', '3']

If we didn't use loop and directly print csv_reader then the output would be the location of object in memory:

import csv
with open('newcsvfile.csv', 'r') as csvfile:
    csv_reader = csv.reader(csvfile)
    print(csv_reader)

Output:

<_csv.reader object at 0x7ff00b730ec0>

Using dictionaries to read and write CSV files:

Python CSV module provides csv classes csv.DictReader amd csv.DictWriter to deal datas as two-values pair. Datas are dealt by mapping them to dictionaries.

csv.DictReader(csvfilename,fieldnames,delimeter) creates an object like regular reader object but this object operates in csv files like fieldnames as keys. fieldnames are the values in the first row of file. fieldnames is optional here.

csv.DictWriter(csvfilename,fieldnames,delimeter) creates an object which functions like regular writer object. fieldnames is not optional here.

import csv
with open('newcsvfile.csv', 'r') as csvfile:
    # Calling DictReader object to read csv file
    csvreader = csv.DictReader(csvfile)
    with open('newentry.csv', 'w') as newentry:
        # defining fieldnames
        fieldnames = [ 'Name', 'Address' ]
        # Calling DictWriter object to write csv file
        csvwriter = csv.DictWriter(newentry, fieldnames=fieldnames, delimiter='|')
       #  writing header which writes fields names as header in first row
        csvwriter.writeheader()
        for line in csvreader:
            # deleting all Roll data to create new csv file
            del line[ 'Roll' ]
             # writing data to newentry.csv after deleting Roll
            csvwriter.writerow(line)

We deleted Roll and wrote data in new csv file named newentry.csv in above code. Here, '|' delimeter is used that means values are separated by '|'. The output file is found here.

Let's see what it prints if we want to print values under fieldnames.

import csv
with open('newcsvfile.csv', 'r') as csvfile:
    # Calling DictReader object to read csv file
    csvreader = csv.DictReader(csvfile)
    for line in csvreader:
       print(line[ 'Name' ])

Output:

Programming
Python
Open

all above codes including previous tutorials codes are available in my Github repo. Click here to download

For more details please visit Python Docs.

Curriculum

Python tutorials for beginners : Part - I

Python tutorials for beginners : Part - II

Python tutorials for beginners : Part - III

Object-oriented Python

Reading and writing to files in python



Posted on Utopian.io - Rewarding Open Source Contributors

Working with CSV files in Python : Part I | Ecency