×

CSV Module In Python

Introduction

CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores tabular data in the form of plain text. Here the commas are used to separate each column within the row and every part of the data is separated with a comma.

CSV is a simple format for data exchange as it is general and, simpler. A CSV file is represented when it is saved with .CSV file extension.

Working with Python CSV files:

Before using the functions to CSV module we have to import it using the below code:
import csv

The CSV library provides various functions to perform read and write operations in the CSV files.

Reading the CSV file:

To perform read operation in CSV file. first, we have to generate the reader object using the reader function.

The Python's built-in function open() returns a file object by which the csv file as a text file is opened then it is passed to the reader.

Here’s the employee_joining.txt file:

name, department,joining month
Raam,Accounting, November
Aakash,IT, March

Below is the code to read the above file:

import csv

with open('employee_joining.txt') 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 joined in {row[2]}.')

            line_count += 1

    print(f'Processed {line_count} lines.')

Output:

Column names are name, department, joining month

Raam works in the Accounting department, and joined in November.

Aakash works in the IT department, and joined in March.

Here, the reader returned each row is a list of string elements and it contains the data obtained by eliminating the delimiters.

Reading a CSV file as a dictionary:

We can perform the reading operation of CSV data directly with the dictionary. Here we can read the csv files using dictreader.

The results are represented as a dictionary in which the header row is placed as the key and other rows are placed as values.

Again, the input file is employee_joining.txt file:

name, department,joining month
Raam,Accounting, November
Aakash,IT, March

Code to read as a dictionary:

import csv

with open('employee_joining.txt', mode='r') as csv_file:

    csv_reader = csv.DictReader(csv_file)

    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["name"]} works in the {row["department"]} department, and joined in {row["birthday month"]}.')

        line_count += 1

    print(f'Processed {line_count} lines.')

Output is the same as before:

Column names are name, department, birthday month

Raam works in the Accounting department, and joined in November.

Aakash works in the IT department, and joined in March.

Additional Python CSV reader parameters:

Different styles of CSV files are handled by the reader object via defining optional parameters.

Some parameters are as shown below:

  • quotechar defines the character that is used to enclose fields that includes the delimiter character. quote (' " ') is the default.
  • delimiter definesthe character that is used to divide each field. comma (',') is default.
  • escapechar defines the character that is used to skip the delimiter character. no escape character is the default.

We can explore these more with the example below.

we have employee_addresses.txt file:

name,address,date joined
Raam,Lajpat nagar New Delhi, 11024, Jan 4
Satyam, Rohini New Delhi, 110085, March 2

This file is consisting of three fields which are name, address, date joined which are separated by commas.

The problem is here is that the address field data is also containing the comma to specify the zip code.

Here, three different ways are possible to overcome this problem:

by using different delimiter:

We can safely use the comma in the data itself by using the different delimiter. we can use the delimiter additional parameter to define the new delimiter.

wrapping the data in quotes:

Here, the quotechar optional parameter can be used to define that the character used for quoting. whereas that character also does not show in the data, everything is fine.

by escaping the delimiter character:

Escape characters work as same as do in format strings and it invalidates the definition of the character being escaped and this is for delimiter character.
escapechar must specify the escape character when it is used.

Writing the CSV file:

The writing of csv file can be done by using .write_row() method and a writer object.

import csv

with open('employee_file.csv', mode='w') as employee_file:

    employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    employee_writer.writerow(['Raam', 'Accounting', 'November'])

    employee_writer.writerow(['Aakash', 'IT', 'March'])

Here, while writing which character is to be used is specified by the quotechar optional character.

It returns the plaintext which shows that the file is created for reading.

Output:

Raam, Accounting, November

Aakash,IT, March

Writing of CSV file from the dictionary:

As we can read the data from the dictionary. also, we can write the data from the dictionary as well.

import csv

with open('employee_file2.csv', mode='w') as csv_file:

    fieldnames = ['emp_name', 'dept', 'birth_joining']

    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

    writer.writeheader()

    writer.writerow({'emp_name': 'Raam', 'dept': 'Accounting', 'joining_month': 'November'})

    writer.writerow({'emp_name': 'Aakash', 'dept': 'IT', 'joining_month': 'March'})

While writing a dictionary the fieldnames parameter is required.

Output:

emp_name,dept,joining_month

Raam,Accounting,November

Aakash,IT, March

Handling CSV files by using Pandas library:

Pandas is the most popular Data science library in Python. It is used for data analysis and data manipulation.

When the amount of data is large it is better to use the Pandas library to handle the CSV files.

we have to install the Pandas before using them and to

know about installation visit: How to install Pandas?

Reading CSV File by using Pandas:

The read_csv() function is employed for reading the CSV files using Pandas.

Example:

import pandas as pd

pd.read_csv("employee.csv")

This code reads the employee.csv from the current directory.

Writing CSV files by using Pandas:

The to_csv() function is employed for writing to the CSV files using Pandas. The to_csv() is DatafFrame's function.

Example:

import pandas as pd

# creating a data frame

df = pd.DataFrame([['Raam', 24], ['Aakash', 22]], columns = ['Name', 'Age'])

# writing data frame to a CSV file

df.to_csv('Age.csv')

Here the DataFrame is created by using pd.Dataframe and then to_csv() function is called for this object.

Output:

Name, Age

Raam, 24

Aakash, 22

Conclusion:

In this article, we have discussed about the Python CSV module and understood how to perform different operations for reading and write data in CSV Files.


Related Topics

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Assertion error in python

In this article, we will discuss assertions in the python programming language. Assertion: Assertions are a way of telling a program to test a certain condition and trigger an error if the...

3 minutes read.

Speech Recognition Module in Python

Speech Module in Python: Converting text to speech, known as Speech Synthesis, this process is the computer-generated recreation of human speech. This module converts the human language text into human-like...

8 minutes read.

Python filter() function

Python filter() function The filter() function constructs an iterator from those elements of iterable for which the parameter ‘function’ returns a Boolean value true. Syntax filter(function, iterable) Parameter function: This parameter represents a function to be run for each item in the...

1 minute read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

Conditional Expressions in Python

In Python conditional expression are sometimes referred to operator called as ternary operator. Not only Python supports ternary operator but many other programming languages supports it. Ternary operator are the...

2 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python enumerate() function

Python enumerate() function The enumerate() function in Python takes a collection (e.g. a tuple) and returns it as an enumerate object. Syntax: enumerate(iterable, start=0) Parameter Iterable:  This parameter represents an iterable object start:    This parameter represents a number defining...

1 minute read.

Poolmanager in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

Introduction to Scratch programming

Scratch programming is generally designed for children who may create digital stories, games, and animations using the computer language Scratch, which has the largest kid-focused community in the world. Scratch...

3 minutes read.

What is Python compiler GDB?

The source code of one programming language is converted into machine code, bytecode, or another programming language by a compiler, a specialised software. A compiler is a tool that converts high-level...

3 minutes read.

Best Way to Learn Python for Free

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. Learning Python is a step towards coding. Python gets...

5 minutes read.