Skip to main content

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

Open a CSV file in Python

A CSV file is a “comma-separated values” file. In plain English, this is a text file that contains an unusually large amount of data. More often than not, this is used in order to create databases of information, where each unit of data is separated by a comma. Hence the name!

Being able to manipulate, load, and store large amounts of data is a hugely beneficial skill when programming. This is particularly true in Python, seeing as Python is such a popular option for machine learning and data science.

Read on then, and we’ll explore how to read CSV files in Python!

How to read CSV files in Python by importing modules

To get started, we’re first going to 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 looks like so:

How to open CSV files in Python

Credit: Adam Sinicki/ Android Authority

If we open this up as a text file, we see it is stored 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 defines the values, and each subsequent line includes three entries!

So, how do we open this in Python? Fortunately, there is no need to build a CSV parser by scratch! Rather, we can simply use ready-made modules. The one we’re interested in is called, you guessed it, CSV!

We do that like so:

import  csv

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

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

We can also split the 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 a CSV file? In that 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 a CSV file is actually just a text document with a fancy formatting. That means that you actually don’t need to use a module if you want to know how to open CSV files in Python!

Also read: How to become a data analyst and prepare for the algorithm-driven future

You can quite simply write to a text file like so:

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

This actually makes it fairly simple to take the contents of a list, dictionary, or set, and turn them into a CSV! Likewise, we could read our files in a similar way and then simply break the data down by looking for commas. The main reason not to do this, is that some CSV files will use slightly different formating, which can cause problems when opening lots of different files. If you’re just working with your own files though, then you’ll have no trouble!

Also read: How to read a file in Python and more

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

What are you going to do with this knowledge? Let us know in the comments below! And if you want to learn more skills like this, then we recommend checking out our list of the best online Python courses. There you’ll be able to further your education with courses like the Python Data Science Bundle. You can get it for $37 right now, which is a huge saving on the usual $115.98!

 

 

 



source https://www.androidauthority.com/how-to-open-csv-file-python-1140486/

Comments

Popular posts from this blog

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...