Reading a CSV file
Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Python’s built-in open() function, which returns a file object. In this example, we first open the CSV file in READ mode, file object is converted to csv.reader object and further operation takes place. Code and detailed explanation is given below.
Python3
# importing csv module import csv # csv file name filename = "aapl.csv" # initializing the titles and rows list fields = [] rows = [] # reading csv file with open (filename, 'r' ) as csvfile: # creating a csv reader object csvreader = csv.reader(csvfile) # extracting field names through first row fields = next (csvreader) # extracting each data row one by one for row in csvreader: rows.append(row) # get total number of rows print ( "Total no. of rows: %d" % (csvreader.line_num)) # printing the field names print ( 'Field names are:' + ', ' .join(field for field in fields)) # printing first 5 rows print ( '\nFirst 5 rows are:\n' ) for row in rows[: 5 ]: # parsing each column of a row for col in row: print ( "%10s" % col, end = " " ), print ( '\n' ) |
Output:
The above example uses a CSV file aapl.csv which can be downloaded from here.
Run this program with the aapl.csv file in the same directory.
- Let us try to understand this piece of code.
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
- Here, we first open the CSV file in READ mode. The file object is named as csvfile. The file object is converted to csv.reader object. We save the csv.reader object as csvreader.
fields = csvreader.next()
- csvreader is an iterable object. Hence, .next() method returns the current row and advances the iterator to the next row. Since, the first row of our csv file contains the headers (or field names), we save them in a list called fields.
for row in csvreader:
rows.append(row)
- Now, we iterate through the remaining rows using a for loop. Each row is appended to a list called rows. If you try to print each row, one can find that a row is nothing but a list containing all the field values.
print("Total no. of rows: %d"%(csvreader.line_num))
- csvreader.line_num is nothing but a counter which returns the number of rows that have been iterated.
Working with csv files in Python
Python is one of the important fields for data scientists and many programmers to handle a variety of data. CSV (Comma-Separated Values) is one of the prevalent and accessible file formats for storing and exchanging tabular data.
In article explains What is CSV. Working with CSV files in Python, Reading, and Writing to a CSV file, and Storing Emails in CSV files.
Table of Content
- What is a CSV File?
- Working with CSV files in Python
- Reading a CSV file
- Reading CSV Files Into a Dictionary With csv
- Writing to a CSV file
- Writing a dictionary to a CSV file
- Reading CSV Files With Pandas
- Writing CSV Files with Pandas
- Storing Emails in CSV files