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
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.