Posted on 16 Dec 2018 06:12:00 (8 months ago)
So in previous post I have shown you the most basic way of reading csv files. Now we will use Python inbuilt module to read the csv files. So, I am using the same data i.e. 'person.csv' which contains the below data,
Name,Place,Date Sam,New York,23102015 Juli,Cape Town,12122017 Avinash,New Delhi,09122016 Rio,Tokio,01072015
So csv inbuilt module contains two methods for parsing data i.e. reader and DictReader. So in this section I will discuss about reader function only. Reader function will get data from the csv file and parse each row in different list and the first row contains the header files.
So below is the code for reader function,
from csv import reader with open('person.csv') as file: data = reader(file) for row in data: print(row)
And when you run the above code you will get each row in list like below,
['Name', 'Place', 'Date'] ['Sam', 'New York', '23102015'] ['Juli', 'Cape Town', '12122017'] ['Avinash', 'New Delhi', '09122016'] ['Rio', 'Tokio', '01072015']
So after that you can manipulate your data or do something we the list and one thing more I want to add to this that reader function accepts an optional delimeter kwarg incase your data isn't seperated by commas.