How to Read CSV File in Python

A CSV (Comma Separated Values) file is a type of plain text file designed to arrange data in a tabular structure in a specific manner. The CSV format is a delimited text file that employs commas to separate individual values.

The entries in the CSV file represent the data log, with each log entry consisting of one or multiple fields, separated by commas. CSV is a widely utilized file format for the purposes of importing and exporting data between spreadsheets and databases.

In Python, we have the capability to read CSV files through the functions provided by the built-in csv module as well as the pandas library. In the subsequent sections, we will examine the various methods available for reading CSV files in Python.

In order to comprehend these methods, we will utilize the subsequent CSV file.

File: companies.csv

Example

Organization,Industry,Employees

Google,IT,1500

Microsoft,IT,1300

Tata,Manufacturing,1000

C# Tutorial,Education,200

Apple,IT,1200

In the CSV file mentioned above, various values are delineated by commas ','. This file will serve as a reference for the examples presented in the upcoming sections.

Reading CSV Files in Python

There are several methods to read a CSV file in Python, utilizing either the CSV module or the pandas library.

  • CSV Module: The csv module is among Python's standard libraries. It provides a range of classes and functions designed to facilitate the reading and writing of tabular data in CSV file format.

In order to utilize the csv module in Python, it is necessary to import it first. The syntax provided below will assist us in employing the csv module for our intended tasks.

Syntax:

Example

import csv
  • Pandas Library: The pandas library is a freely available Python library utilized for data manipulation and analysis. It stands out as one of the most crucial tools in Python, enabling data scientists, analysts, and developers to engage with structured datasets.

In a manner akin to the csv module, it is necessary to import pandas before utilizing it. Below is the syntax required to incorporate the pandas library into your Python scripts.

Syntax:

Example

import pandas

We will now explore various methods available for reading CSV files in Python.

1) Read CSV Files Using the csv.reader Method

The function csv.reader is included in Python's built-in csv module. Its primary purpose is to facilitate the reading of data from a CSV file. When invoked, it produces a reader object that allows for the iteration over the lines contained within the specified CSV file.

Python Example to read CSV files using the csv.reader Method

Let’s examine an instance of utilizing the csv.reader function.

Example

Example

import csv

# opening the CSV file

with open('companies.csv', newline='') as file:

    # using the reader() function to read the content of the file

    reader = csv.reader(file)

    # printing each row of the table

    for row in reader:

        print(row)

Output:

Output

['Organization', 'Industry', 'Employees']

['Google', 'IT', '1500']

['Microsoft', 'IT', '1300']

['Tata', 'Manufacturing', '1000']

['Example', 'Education', '200']

['Apple', 'IT', '1200']

Explanation:

In the preceding illustration, we included the csv module and employed the 'with open' statement to access the companies.csv file. Subsequently, we utilized the csv.reader function to extract the data from the CSV file. Finally, we displayed the contents of the file, presenting it row by row.

2) Read CSV Files Using the csv.DictReader Method

Utilizing the csv.DictReader class allows us to transform CSV files into dictionaries, with the field names serving as the keys. The filesystem provides straightforward access to the value fields, enhancing the readability of the data. When iterating through the data, this class outputs each row as a dictionary.

Python Example to Read CSV Files using the csv.DictRead Method

Here is an example of the csv.DictReader class:

Example

Example

import csv

# opening the CSV file

with open('companies.csv', newline='') as file:

    # using the DictReader() class to convert the content of the file into a dictionary

    reader = csv.DictReader(file)

    # printing each row of the table

    for row in reader:

        print(row)

Output:

Output

{'Organization': 'Google', 'Industry': 'IT', 'Employees': '1500'}

{'Organization': 'Microsoft', 'Industry': 'IT', 'Employees': '1300'}

{'Organization': 'Tata', 'Industry': 'Manufacturing', 'Employees': '1000'}

{'Organization': 'Example', 'Industry': 'Education', 'Employees': '200'}

{'Organization': 'Apple', 'Industry': 'IT', 'Employees': '1200'}

Explanation:

In the preceding example, we imported the csv module. Subsequently, we utilized the 'with open' statement to access the CSV file. Following that, we employed the DictReader class from the csv module to convert each row into a dictionary based on its column headers. Finally, we displayed the data in a dictionary format.

3) Read CSV Files Using the pandas.read_csv Method

Pandas is an open-source library that leverages NumPy, designed to facilitate the manipulation and analysis of data. It streamlines the various processes involved in data handling. The read_csv function enables users to import CSV files directly as DataFrames, thereby aiding in efficient time management and providing a structured approach to data organization.

Python Example to Read CSV Files using the pandas.read_csv Method

Let's explore an example that illustrates the process of reading a CSV file with the help of the pandas.read_csv function in Python.

Example

Example

import pandas as pd

# Reading the CSV file into a DataFrame

dframe = pd.read_csv('companies.csv')

# Displaying the DataFrame

print(dframe)

Output:

Output

Organization       Industry  Employees

0       Google             IT       1500

1    Microsoft             IT       1300

2         Tata  Manufacturing       1000

3  C# Tutorial      Education        200

4        Apple             IT       1200

Explanation:

In this section, we have brought in the pandas library. Following that, we utilized the read_csv function to transform the specified CSV file into a DataFrame and assigned it to a variable. Finally, we employed the print function to display the DataFrame.

Consequently, the CSV file is transformed into a format consisting of rows and columns. This arrangement simplifies our comprehension of the data contained within the CSV file.

Conclusion

Python is a highly adaptable and robust programming language that provides numerous techniques for reading CSV files. In this tutorial, we explored several of these techniques. We examined how the csv module and the pandas library facilitate the process of reading CSV files. We discussed the various functions and classes within the csv module that enable us to import CSV files in different formats. Likewise, the read_csv function from the pandas library allows us to present the contents of a CSV file in a dictionary format.

Python Read CSV File FAQs

1. What is the Python csv module used for?

The csv module serves the purpose of both reading from and writing to CSV (Comma-Separated Values) files. It facilitates the handling of structured data presented in a simple text format.

2. How do we read a CSV file using the csv module?

You have the option to utilize reader for retrieving rows in the form of lists, or you can employ csv.DictReader to obtain rows structured as dictionaries, utilizing the column headers as their respective keys.

3. How do we write data to a CSV file using Python?

Utilize the writer function to input data as rows within a CSV file. You have the option to write data in the form of lists by employing either the writerow or writerows methods.

4. What is the difference between reader and csv.DictReader?

The function csv.reader processes each row and outputs it as a list, whereas csv.DictReader interprets each row as a dictionary, utilizing the keys derived from the initial row (header).

5. Do we need external libraries like Pandas to work with CSV files?

For fundamental operations, Python's native csv module is adequate. Nevertheless, when it comes to more complex data analysis, libraries such as Pandas provide a broader range of functionalities.

Input Required

This code uses input(). Please provide values below: