CSV file is one of most common used format we encounter. Let see few technique to efficiently import these data to python for better & efficient use.
CSV is also known as comma separated format. you can easily write a code to import CSV data .
But today we will see some of very efficient way to do so.
Python provide few package like CSV, unicodecsv, pandas Thease packages help you to do your job.
CSV is also known as comma separated format. you can easily write a code to import CSV data .
But today we will see some of very efficient way to do so.
Python provide few package like CSV, unicodecsv, pandas Thease packages help you to do your job.
- CSV : package follow basic approach & slow , it reads data line by line & split.
- unicodecsv : Import data & create a list of dictionaries each data is associated with its attribute as key. So it coud be very usefull in many cases but this process is also slow
- pandas : This technique is very fast as compare to above two methods.
You can download & test in your computer using Ipython notebook [Github_link]
Example
In [2]:
import unicodecsv
import pprint
import csv
In [3]:
csv_file_name="enrollments.csv"
Json Import of csv file¶
Import data in json formate, All data are imported as string¶
In [16]:
enrolment = []
f=open(csv_file_name,'rb')
reader =unicodecsv.DictReader(f)
#reader is a itterater so loop is possible only once
print "type(reader) =",reader
#for each_row in reader:
# enrolment.append(each_row)
enrolment=list(reader) #shorthand for above two line
#close file
f.close()
print "Total no of row : ",len(enrolment),"\n\n"
#print demo data
pprint.pprint(enrolment[1])
In [23]:
data = []
with open(csv_file_name, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in reader:
data.append(row)
print "Headings :",(data[0])
print"Data :",(data[1])
Using Pandas ( Very fast than above two)¶
In [4]:
import pandas as pd
In [7]:
enrol = pd.read_csv(csv_file_name)
In [10]:
enrol
Out[10]:
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT