How to Read CSV File in Python

To read a csv file in Python, use the csv.reader() function. The csv.reader() function reads the file, which returns an iterable reader object. The reader object is then iterated using a for loop to print the contents of each row.

CSV stands for Comma Separated Values. It is a file consisting of values separated by values. To work with the CSV package in Python, import a package. Then you can call the functions of that package. For example, the csv package is mainly used for working with csv files.

Pandas is also a Python library used in projects related to machine learning.

Program for reading a csv file using csv package

You can download the sample.csv file here which we will use in the below program.

import csv

with open("sample.csv", "r") as file:
  data = csv.reader(file)
  for field in data:
    print(field)

Output

  Series_reference   Period    ...   Series_title_4      Series_title_5
0 BDCQ.SF1AA2CA     2016.06    ...    Unadjusted           NaN
1 BDCQ.SF1AA2CA     2016.09    ...    Unadjusted           NaN
2 BDCQ.SF1AA2CA     2016.12    ...    Unadjusted           NaN
3 BDCQ.SF1AA2CA     2017.03    ...    Unadjusted           NaN
4 BDCQ.SF1AA2CA      2017.06   ...    Unadjusted           NaN
... ... ... ... ... ...
5435 BDCQ.SF8RSCA 2020.12      ...    Unadjusted           NaN
5436 BDCQ.SF8RSCA 2021.03      ...    Unadjusted           NaN
5437 BDCQ.SF8RSCA 2021.06      ...    Unadjusted           NaN
5438 BDCQ.SF8RSCA 2021.09      ...    Unadjusted           NaN
5439 BDCQ.SF8RSCA 2021.12      ...    Unadjusted           NaN

[5440 rows x 14 columns]

In this program, we imported a package called the csv. It is used for doing csv file operations. Then, we have opened the sample.csv in the read mode.

In the next line, we read the data from the csv file using a csv.reader() function. The reader() function reads the content from the csv file. The content read from the sample.csv is stored in a data variable. 

Every row present in the csv file is displayed using a for loop. Hence we have used the reader method to read data from the csv file.

import csv

with open("sample.csv", "r") as file:
  data = csv.reader(file, delimiter="\t")
  for field in data:
    print(field)

In this program, we gave a delimiter based on which the csv file is separated into rows and columns. In general, the delimiter will be a comma. If we want a different delimiter, we can give it in the delimiter attribute. In this program, we have given \t as the delimiter.

Program for reading a csv file using Pandas package

To read a comma-separated values file into DataFrame in Python, use the pandas.csv() function. Pandas read_csv() function can be used in different ways as per necessity like using custom separators, reading only selective columns/rows, and so on.

import pandas as pd

data = pd.read_csv("sample.csv")
print(data)

In this program, we imported Pandas library. Pandas is a library for working with files. We can work with all kinds of files using pandas. In this program, we have used pd.read_csv() function to read a csv file.

CSV files are most widely used as data sets for machine learning projects. These csv files are read and stored in a data frame. The DataFrame is similar to the table. We can work with several operations on the dataframe using pandas. 

In the next line, we read the content of the csv file using a function called the read_csv(). The read_csv() function is used for reading a csv file.

We read the contents from the csv file and stored it in a variable called data. We then print the data in the file. Hence, we used pandas and csv files to read a csv file.

That’s it for this tutorial.

See also

How to Tabulate JSON in Python

How to Convert File to String in Python

How to Write JSON File in Python

Leave a Comment