How to open CSV files in Python – save and retrieve large data sets

How to open CSV files in Python - save and retrieve large data sets

CSV files are “comma separated values” files. In plain English, this is a text file that contains a large amount of data. More often than not, this is used to create a database of information, where each data unit is separated by commas. Then the name!

Being able to manipulate, load, and store large amounts of data is a very useful skill when programming. This is especially true in Python, considering that Python is a popular option for machine learning and data science.

Read on, and we will explore how to read CSV files in Python!

How to read CSV files in Python by importing a module

To begin, we will first create our CSV file.

You can do this in Excel by creating a simple spreadsheet and then choosing to save it as a CSV file. I made a little list of exercises, which looked like this:


If we open this as a text file, we see it saved like this:

Type of Exercise,Sets and Reps,Weight
Bench press,3 x 3,120kg
Squat,3 x 3,100kg
Deadlift,3 x 3,150kg
Curls,3 x 5,25kg
Bent rows,3 x 5,80kg
Military press ,3 x 5,60kg

The top line determines the value, and each subsequent line includes three entries!

So, how do we open this in Python? Luckily, there’s no need to create a CSV parser from scratch! Instead, we only need to use the ready-made modules. What we’re interested in is called, you guessed it, CSV!

We do it like that:

import  csv

Now, we can open the CSV file and print the data to the screen:

with open('c:\\Python\\Exercises.csv') as csv_file:
    csvFile = csv.reader(csv_file, delimiter=',')
    for row in csvFile:

We can also share data if we want to do fancy things with it:

for row in csvFile:
        if lineCount > 0:
            print(f'Perform {row[0]} for {row[1]} sets and reps, using {row[2]}.')
        lineCount += 1

As you can see, this will simply run through the file, extract each piece of data, and then write it out in plain English.

Or, what if we want to pull out a specific row?


    for row in csvFile:
        if lineCount == 2:
            print(f'Perform {row[0]} for {row[1]} sets and reps, using {row[2]}.')
        lineCount += 1

Finally, what if we want to write to CSV file? In this case, we can use the following code:


    with open('C:\\Python\\Exercises2.csv', mode='w') as trainingRoutine:
        trainingRoutine = csv.writer(trainingRoutine, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        trainingRoutine.writerow(['Exercise', 'Sets and Reps', 'Weight'])
        trainingRoutine.writerow(['Curls', '3 x 5', '25kg'])
        trainingRoutine.writerow(['Bench Press', '3 x 3', '120kg'])

How to open CSV files in Python manually

Remember that CSV files are actually just fancy text documents. That means that you don’t actually need to use a module if you want to know how to open CSV files in Python!

You simply write to the text file like:


myFile = open("Exercises3.csv", "w+")
myFile.write("Exercise,Sets and Reps,Weight\nCurls,3 x 5,25kg\nBench Press,3 x 3,120kg")

This actually makes it simple enough to take the contents of a list, dictionary, or set, and convert it to CSV! Likewise, we can read our files in the same way and then break up the data by looking for commas. The main reason for not doing this, is that some CSV files will use a slightly different format, which can cause problems when opening many different files. If you only work with your own files, then you will have no trouble!

And so: now you know how to open CSV files in Python! And with that, you have tried your first bit of JSON development and even a bit of data science. Feel proud!

Related posts

How to use the chmod command on Linux


How to Install Spotify on Ubuntu 20.04


How to Upgrade From Windows 7 to Linux


How to Install the SNMP Cacti Monitoring Tool on Debian 10


How to get started with Kali Linux in 2020


How to create files in Python and more


How To Install NTP (Chrony) On CentOS 8 / CentOS 7 & RHEL 8 / RHEL 7


How to Install and Configure the mod_pagespeed Module with Apache on Ubuntu 20.04 LTS


How to add a DICOM image display to Nextcloud Hub