How to read a CSV file in Python

0

Most of the tabular data available for processing are in csv format. So it is important that you know about reading CSV files in Python.

read csv file in python

Read a CSV file in Pyhon

With Python CSV module, You can read csv files and get the content of the CSV file. Python csv module, though work in many cases but If you are working on a large dataset then It might not be a good way to read those huge and multiple files using csv module.

So in this Article I will tell you all the possible ways to read CSV files in Python


Read CSV file with Python csv module

The easiest way to read a csv file with Python is to use the csv module. csv is a python built-in module for reading and writting a csv files.

    To read a CSV file with Python csv module
  • use the open() function and get the file object
  • use the csv.reader() function and convert the file object tocsv_reader object
  • You can now Iterate through the csv_reader object to get the individual row of the csv file
Python Program that use the python csv module to read csv file
import csv 
with open('csvfile.csv','r') as csv_file:
    csv_reader = csv.reader(csv_file)
    print(type(csv_reader))
    
    for row in csv_reader:
        print(row)

The above python program will read each csv file and print it. The output of the above code is a rows out of a csv files.


Output of the Program
['BDCQ.SF1AA2CA', '2019.09', '1381.514', '', 'F', 'Dollars', '6']
['BDCQ.SF1AA2CA', '2019.12', '1370.985', '', 'F', 'Dollars', '6']
['BDCQ.SF1AA2CA', '2020.03', '1073.017', '', 'F', 'Dollars', '6']
['BDCQ.SF1AA2CA', '2020.06', '1131.445', '', 'F', 'Dollars', '6']
['BDCQ.SF1AA2CA', '2020.09', '1440.101', '', 'F', 'Dollars', '6']
['BDCQ.SF1AA2CA', '2020.12', '1489.979', '', 'F', 'Dollars', '6']

Python Pandas Module to read CSV files

Python pandas module is specifically built to work with large and huge datasets. Reading a csv files with pandas is easy and the pandas module in Python gives you more control over reading and writting a csv file.

To read a CSV file in Python with Pandas module

  • install pandas module using pip install pandas
  • import pandas module using import pandas
  • use the pandas read_csv() function
Below is the pyhton code that use pandas module to read a csv file.
import pandas as pd

df = pd.read_csv('csvfile.csv', sep=',')
print(df)

Output of the Code
0     BDCQ.SF1AA2CA  2016.06    1116.386         NaN      F  Dollars          6
1     BDCQ.SF1AA2CA  2016.09    1070.874         NaN      F  Dollars          6
2     BDCQ.SF1AA2CA  2016.12    1054.408         NaN      F  Dollars          6
3     BDCQ.SF1AA2CA  2017.03    1010.665         NaN      F  Dollars          6
4     BDCQ.SF1AA2CA  2017.06    1233.700         NaN      F  Dollars          6
5     BDCQ.SF1AA2CA  2017.09    1282.436         NaN      F  Dollars          6

The above code will convert the csv file into dataframe. With dataframe we can do many things. We can convt it to a python list, python tuple or even convert it to a dictionary.


Converting a Pandas dataframe into a tuples

Look at how beautifuly the pandas module have converted a csv file into a dataframe. We can now do many things to this dataframe. Let me show you how you can convert this Dataframe into a tuples.

To convert a pandas dataframe into a tuples you can use the follwoing list comprehension.
import pandas as pd
df = pd.read_csv('csvfile.csv', sep=',')
list_tuples = [tuple(x) for x in df.values]
print(list_tuples)

Above code will convert each row of a csv file into a tuple. You can interate through the list to read each tuple.{alertSuccess}


Convert csv file data into list with Pandas

Using Python pandas module, we can convert the the content of a csv file into a python list. To convert a csv file into a python list using python module. You can use the list compreshension. The follwoing example code will convert each row into a python list. This way we will have a list of lists.

Python example of converting a csv file into python list
import pandas as pd
df = pd.read_csv('csvfile.csv', sep=',')
list_of_lists = [list(x) for x in df.values]
print(list_of_lists)

Why you should use Pandas for Reading CSV file

Python provide to many options for reading csv files. But many developers use Pandas, one reason can be that it is always there and already imported but there are other reasons as well.

Reasons why you should use Pandas for reading csv files
  • It automatically deals with headers
  • it loads the file directly from the path and does not expect a file pointer
  • it has better "export" options

Read a CSV file without any Python module

Python provide module for a reason. There are so many exceptions that can occur and so many errors and bugs. Python module are developed by experience programs and they have kept everything in mind while creating these modules. But if you are still thihnking about reading a csv file without using any module. check out the follwoing program.

rows = []
with open('csvfile.csv') as f:
    for line in f:
        # strip whitespace
        line = line.strip()
        # separate the columns
        line = line.split(',')
        # save the line for use later
        rows.append(line)
print(rows)

with the above python program we can read csv file without using any moudles. though this is a very naive and not recommened way of doing so.


Python Dask module to read CSV files

Dask is a Python module that have some extra feature of python pandas module. They have added some multi-threading capablities in Dask Module. If you are already using dask then you should know that with dask we can read a csv file more faster.

import dask.dataframe as dk

dk_df=dk.read_csv('csvfile.csv')
print(dk_df)

Convert a CSV file into a table form in Python

There are many ways you can convert a csv file into a table form. you can use pandas and other python modules. But the best way to convert a csv file into a table is to use the littletable module.

To convert a csv file into a table in python use the follwoing steps
  • install the littletable module
  • import the littletable module
  • use the Table() to convert a csv file into an awesome table
Below is the python code that can convert a csv file into a talbe
import littletable as lt
tbl = lt.Table().csv_import('csvfile.csv')
tbl.present()

Output of the code is the follwoing beautiful table.
  Series_Reference   Period    Data_Value   Suppressed   Status   Units     Magnitude 
 ─────────────────────────────────────────────────────────────────────────────────────
  BDCQ.SF1AA2CA      2016.06   1116.386                    F      Dollars       6
  BDCQ.SF1AA2CA      2016.09   1070.874                    F      Dollars       6
  BDCQ.SF1AA2CA      2016.12   1054.408                    F      Dollars       6
  BDCQ.SF1AA2CA      2017.03   1010.665                    F      Dollars       6
  BDCQ.SF1AA2CA      2017.06   1233.7                      F      Dollars       6
  BDCQ.SF1AA2CA      2017.09   1282.436                    F      Dollars       6

Skip header row of a csv file in Python

When we are reading a file with csv moudle. It also include the header row in it. We can skip the header row by using the next() funciton. The csv.reader() funciton returns a gernarator and using the next() move the pointer to the next row, by skipping the first one.

Use the follwoing code to skip the header row of a csv file in python.
import csv

with open('csvfile.csv','r') as csv_file:
    # csv_reader contains a gernarator
    csv_reader= csv.reader(csv_file)
    # to skip the header use the next() method
    header = next(csv_reader)
    # print header 
    print(header)
    rest_csv_data = csv_reader
    for row in rest_csv_data:
        pass

Convert a CSV file into Database Table in Pyhton

Using the sqlite3 and csv modules in python, we can convert a csv file into a database table. You should also visit converting a csv file into a database table in python.

Summary and conclusion:

In this article we have learned the differnt ways of reading csv files. I have shown you the different ways of reading csv files in python. We have also see how we can convert a csv file into a table that can be more readable in the terminal. If you still have any quesiton please let me know in the comment section.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !