Working with csv files in Python
This article explains how to load and parse a CSV file in Python.
What is a CSV?
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.
For working CSV files in Python, there is an inbuilt module called csv.
Working with csv files in Python
Example 1: Reading a CSV file
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.
Example 2: Writing to a CSV file
Let us try to understand the above code in pieces.
- fields and rows have been already defined. fields is a list containing all the field names. rows is a list of lists. Each row is a list containing the field values of that row.
with open(filename, 'w') as csvfile: csvwriter = csv.writer(csvfile)
- Here, we first open the CSV file in WRITE mode. The file object is named as csvfile. The file object is converted to csv.writer object. We save the csv.writer object as csvwriter.
- Now we use writerow method to write the first row which is nothing but the field names.
- We use writerows method to write multiple rows at once.
Example 3: Writing a dictionary to a CSV file
In this example, we write a dictionary mydict to a CSV file.
with open(filename, 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames = fields)
- Here, the file object (csvfile) is converted to a DictWriter object. Here, we specify the fieldnames as an argument.
- writeheader method simply writes the first row of your csv file using the pre-specified fieldnames.
- writerows method simply writes all the rows but in each row, it writes only the values(not keys).
So, in the end, our CSV file looks like this:
Consider that a CSV file looks like this in plain text:
- We notice that the delimiter is not a comma but a semi-colon. Also, the rows are separated by two newlines instead of one. In such cases, we can specify the delimiter and line terminator.
Example 4: How Store Emails in CSV files ?
Here we are importing the csv module and then simply using the same concept of storing the emails in the form of comma-separated entity also with their names. We’re opening the file open() function and specifying that we need that as a csv file and then writing the each column into the csv file using writer object.
Please Login to comment...