How to read CSV file in python?

Reading CSV file using python

What is a CSV file?

CSV stands for “Comma Separated Value”.CSV files are in the form of plain text Document that uses a specific format to organize table information.It is most popular file format used for spreedsheets and databases.

How to read CSV file?

CSV files can be read using CSV modules or Pandas library

 Using CSV Modules : This module provides classes for reading and writing table information in csv file format.Here csv.reader() function is used to read the csv file.

 

import csv
with open('Socialmedia.csv',mode='r')as file:
    csvFile = csv.reader(file)
    for lines in csvFile:
        print(lines)

OUTPUT :

['Social Media','Number of users','Ranking']
['Instagram','2.5 Billion','3']
['Facebook','2.9 Billion','1']
['Whatsapp','2.78 Billion','2']
['Snapchat','414 Million','6']
['TWitter','450 Million','5']
['Tinder','75 Million','7']
['YouTube','2.49 Billion','4']

Using panads library:It is a open-source python library that provides high performance and convenient data structure.Here read_csv() method is used to read the csv files.

import pandas
Data = pandas.read_csv('Socialmedia.csv')
print(Data)

OUTPUT:

 Social Media  Number of users  Ranking
0 Instagram     2.5 Billion       3
1 Facbook       2.9 Billion       1
2 Whatsapp      2.78 Billion      2
3 Snapchat      414 Million       6
4 Twitter       450 Million       5
5 Tinder       75 Million         7
6 YouTube      2.49 Billion       4

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top