Reading a CSV file using Python

Problem

You wish to open a CSV file in your Python program in order to read some data that are not volatile. In this post we will see how to use one of the libraries out there that is designed to read/save different file types like CSV, Excel, Json.

Solution

The pandas library. Let’s assume there is a CSV file with the following content:

cars, color
Acura, blue
Alfa Romeo, white
Aston Martin, white
Audi, black
Bentley, gold
BMW, green
Buick, yellow

The csv file is named ‘data.csv’ and is placed in the same directory with the Python program. The code to read it is as follows:

import pandas as pd

data_frame = pd.read_csv('data.csv')

print(data_frame.to_string()) 

To execute the above:

python3 open_csv.py

The output:

Using different delimiter

What if the file is not comma separated but with a semicolon or a tab? As per read_csv() documentation, the delimiter symbol can be passed as an argument. For example, if we change the input ‘data.csv’ to the following:

airplane    max_altitude
Turboprop Aircraft    35,000 feet
Piston Aircraft    15,000 feet
Jets    49,000 feet

Now there are 2 columns and 3 lines. Each line has a tab character (\t) as separator for the values. We used this due to the use of comma as part of the value in the max_altitude column. The change needed in the code is simply passing the delimiter =’\t’ as shown below:

import pandas as pd

data_frame = pd.read_csv('data.csv', delimiter='\t')

print(data_frame.to_string())

Running it:

python3 open_csv.py

The output is:

ignoring header

The first line usually is used for indicating the headers. What if you wish to ignore it or it doesn’t exist in the first place? It can be done by passing the header=None argument as follows:

import pandas as pd

data_frame = pd.read_csv('data.csv', delimiter='\t',header=None)

print(data_frame.to_string())

Conclusion

Python and pandas library is a great combination and choice when it comes to tasks like reading a text file or saving it. You can have a look on their getting started page for more information.

See more Python tutorials.


0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x