Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How to Read CSV File in Python?

Last updated on Nov 28,2024 20.6K Views

19 / 62 Blog from Python Fundamentals

Do you know what mechanism works behind storing tabular data into a plain text file? The answer is CSV(Comma Separated Values) file which allows putting data into a plain-text format. In this article on “How to Read CSV File in Python”, we will be learning how to read, write and parse a CSV file in Python.

The following aspects will be discussed in detail:

Let’s get started.

What is a CSV File and its uses?

A CSV(Comma Separated Values) is a plain-text file format used to store tabular data such as a spreadsheet or a database. It essentially stores a tabular data which comprises of numbers and text into plain text. Most of the online services give users the liberty to export data from the website into CSV file format. CSV Files generally open into Excel and nearly all the databases have different specific tools to allow the import of the same.

Every line of the file is called a record. And each record consists of fields that are separated by commas which are also known as “delimiter” which is the default delimiter, others include pipe(|), semicolon(;). Given below is a structure of a Normal CSV File separated by a comma, I am making use of a titanic CSV file.

Structure

Passenger,Id,Survived,Pclass,Name,Sex.Age
1,0,3 Braund, Mr. Owen Harris ,male, 22
2,1,1 Cumings, Mrs. John Bradley (Florence Briggs Thayer), female,38
3,1,3 Heikkinen, Miss. Laina ,female, 26
4,1,1 Futrelle, Mrs. Jacques Heath (Lily May Peel),female,35

Moving on let’s talk about the reason behind usage of CSV File format.

Why is CSV File Format Used?

CSV is a plain-text file which makes it easier for data interchange and also easier to import onto spreadsheet or database storage. For example: You might want to export the data of a certain statistical analysis to CSV file and then import it to the spreadsheet for further analysis. Overall it makes users working experience very easy programmatically. Any language supporting a text file or string manipulation like Python can
work with CSV files directly.


Moving ahead, let’s see how Python natively uses CSV.

Python CSV module

Python uses a CSV package which is a part of the standard library, so you need not install it.

import csv

Now let me show you the different CSV functions.

CSV Module Functions

Under the CSV module, you can find the following functions:

Functions

Description

csv.field_size_limit

It returns the maximum field size

csv.get_dialect

Fetches the dialect associated with name

csv.list_dialects

Displays all the registered dialects

csv.reader

Read data from csv file

csv.register_dialect

Dialect associated with a name

 csv.writer

Writes data to a csv file

 csv.unregister_dialect

It deletes the dialect associated with the name dialect registry

 csv.QUOTE_ALL

Quotes everything irrespective of the type

 csv.QUOTE_MINIMAL

Quotes special character field

csv.QUOTE_NONNUMERIC

Quotes fields that are not numeral

csv.QUOTE_NONE

  Doesn’t quote anything in output

Let’s move ahead and see from the coding perspective of the different operations on the CSV file in Python.

Operations On CSV file in Python

You can perform several manipulations once a CSV file is loaded. I am going to show the read and write operations on a CSV file in Python.

 Read CSV file in Python:

import csv 

with open('Titanic.csv','r') as csv_file: #Opens the file in read mode
    csv_reader = csv.reader(csv_file) # Making use of reader method for reading the file


    for line in csv_reader: #Iterate through the loop to read line by line
        print(line)

Output:


Read CSV file in Python

 

Here, as you can see from the output, I have made use of Titanic CSV File. And all the fields are separated by a comma, File is read into Python.

Moving ahead, let’s see how you can write to a CSV file.

Write to CSV file in Python:

import csv

with open('Titanic.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    with open('new_Titanic.csv', 'w') as new_file: # Open a new file named 'new_titanic.csv' under write mode
        csv_writer = csv.writer(new_file, delimiter=';') #making use of write method

        for line in csv_reader: # for each file in csv_reader
            csv_writer.writerow(line) #writing out to a new file from each line of the original file

Output:
Write-CSV-read CSV File in Python-Edureka
Now this way of working with CSV file using a reader and writer method is one of the most common approaches. Let’s move on and see how you can do the same thing using a python dictionary.

Read CSV file as Dictionary:

import csv 

with open('Titanic.csv','r') as csv_file: #Open the file in read mode
    csv_reader = csv.DictReader(csv_file) #use dictreader method to reade the file in dictionary


    for line in csv_reader: #Iterate through the loop to read line by line
        print(line)

Output:
Read-csv-dict-How to read CSV file in Python-Edureka
As you can see from the output, field has been replaced and they now act as a ‘key’ of dictionary.

Let’s see how we can write to a CSV file as dictionary.

Write to CSV file as Dictionary

import csv 

mydict = [{'Passenger':'1', 'Id':'0', 'Survived':'3'}, #key-value pairs as dictionary obj
          {'Passenger':'2', 'Id':'1', 'Survived':'1'},
          {'Passenger':'3', 'Id':'1', 'Survived':'3'}]


fields = ['Passenger', 'Id', 'Survived'] #field names

filename = 'new_Titanic.csv' #name of csv file

with open('new_Titanic.csv', 'w')as new_csv_file: #open a new file 'new_titanic,csv' under write mode
    writer = csv.DictWriter(new_csv_file, fieldnames=fields) 
    writer.writeheader() #writing the headers(field names)

    writer.writerows(mydict) #writing data rows

Output:
Write-csv-dict-How to read csv in python-Edureka

 

Let’s see how to read a CSV file in python as pandas.

Read CSV file as Pandas:

import pandas #install pandas package

result = pandas.read_csv('Titanic.csv') #read the csv file

print(result) # print result

Output:
Read-csv-pandas-How to read csv file in python-Edureka

This brings us to the end of our article “How to read CSV File in Python”. I hope you are clear with all the concepts related to CSV, how to read and write it,  how to read and write CSV as a dictionary and how to read CSV as pandas.

    Make sure you practice as much as possible and revert your experience.

    Got a question for us? Please mention it in the comments section of this “How to read CSV File in Python” article and we will get back to you as soon as possible. To get in-depth knowledge of Python along with its various applications, you can enroll now with our live Python course training with 24/7 support and lifetime access.

    Upcoming Batches For Python Programming Certification Course
    Course NameDateDetails
    Python Programming Certification Course

    Class Starts on 28th December,2024

    28th December

    SAT&SUN (Weekend Batch)
    View Details
    Python Programming Certification Course

    Class Starts on 25th January,2025

    25th January

    SAT&SUN (Weekend Batch)
    View Details
    Comments
    0 Comments

    Join the discussion

    Browse Categories

    webinar REGISTER FOR FREE WEBINAR
    REGISTER NOW
    webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

    Subscribe to our Newsletter, and get personalized recommendations.

    image not found!
    image not found!

    How to Read CSV File in Python?

    edureka.co