Everyday data scientist around the world needs to Read a CSV in Python read file line by line when analyzing data. Luckily, Python has a native library to Read this file format and others.
This blog post shows everything you need to know about how to read and also describes how to write a file in CSV format.
1. Read a CSV File Line by Line in Python
Reading a CSV file is a common task in data analysis. Luckily python contains a native library to read and write CSV files.
Here we assume the following CSV input file:
City Name,Country Name,Rating
San Diego,USA,A
San Francisco,USA,A-
Tokyo,Japan,A+
Rio de Janeiro,Brazil,B+
And use the code below to read ‘myfile.txt’
import csv
with open('myfile.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
print(row)
Which prints
['City Name', 'Country Name', 'Rating']
['San Diego', 'USA', 'A']
['San Francisco', 'USA', 'A-']
['Tokyo', 'Japan', 'A+']
['Rio de Janeiro', 'Brazil', 'B+']
As you can see, the CSV library breakdown for you each row into a list.
2. Read a CSV File into a Dictionary
You can also read your CSV file into a dictionary (technically an OrderedDict), and access the data based in the dictionary keys as you would do with a regular dictionary.
import csv
with open('myfile.txt') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
for row in csv_reader:
print(row)
which prints
OrderedDict([('City Name', 'San Diego'), ('Country Name', 'USA'), ('Rating', 'A')])
OrderedDict([('City Name', 'San Francisco'), ('Country Name', 'USA'), ('Rating', 'A-')])
OrderedDict([('City Name', 'Tokyo'), ('Country Name', 'Japan'), ('Rating', 'A+')])
OrderedDict([('City Name', 'Rio de Janeiro'), ('Country Name', 'Brazil'), ('Rating', 'B+')])
3. Writing a CSV file
Here, you can use csv.writer to create your CSV file object and csv_writer.writerow to input a list and write as a CSV to your output file. It is important to point out here that you can pass a list with floats and/or int without the need to convert the values to string type as done when using the python write() method.
import csv
with open('my_csv_2.csv', 'w') as csv_object:
csv_writer = csv.writer(csv_object, delimiter=',')
csv_writer.writerow(['Osaka', 'Japan', 'A+', 1])
csv_writer.writerow(['Buenos Aires', 'Argentina', 'B', 2])
csv_writer.writerow(['San Jose', 'Costa Rica', 'A', 3])
4. Reading and Writing a Tabular file
Now that you know how to read and write CSV files, the same can be done for tabular files by setting up the csv.reader() function parameter delimiter=’\t’.
I hope this tutorial helps you to Read a CSV in Python.
5. Skip the header of a file with Python
Use the function next on the file object, so it will skip the CSV header when reading it. As you can see below, I added next(csv_reader) before the for loop.
import csv
with open('myfile.txt') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
next(csv_reader)
for row in csv_reader:
print(row)
More Resources
Here are three of my favorite Python Books in case you want to learn more about it.
- Python Cookbook, Third Edition by David Beazley and Brian K. Jones
- Learning Python, 5th Edition Fifth Edition by Mark Lutz
- Python Pocket Reference: Python In Your Pocket by Mark Lutz