How to open CSV files in Python

Open a CSV file in Python

A CSV file is a value file separated by commas. In plain language, this is a text file that contains an unusually large amount of data. Most often this is used to create databases of information with each unit of data separated by a comma. Therefore the name!

The ability to manipulate, load, and save large amounts of data is an extremely useful skill in programming. This is especially true of Python because Python is such a popular option for machine learning and data science.

Then read on and we’ll explore how to read CSV files in Python!

Reading CSV files in Python by importing modules

First we create our CSV file.

You can do this in Excel by creating a simple spreadsheet and then saving it as a CSV file. I made a small list of exercises that looks like this:

How to open CSV files in Python

Recognition: Adam Sinicki / Android Authority

When we open this as a text file we see that it is saved as follows:

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 contains three entries!

How do we open that in Python? Fortunately, there is no need to create a CSV parser from scratch! Rather, we can simply use ready-made modules. The one we’re interested in means you guessed it, CSV!

We do it:

import  csv

Now we can open the CSV file and print this data on the screen:

with open('c:\Python\Exercises.csv') as csv_file: csv = 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 just run through the file, extract each data item and then write it out in plain English.

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

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

What if we want to write to a CSV file? In this case we can use the following code:

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

How to manually open CSV files in Python

Remember that a CSV file is really just a text document with fancy formatting. That means you don’t actually have 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 algorithmic future

You can easily write to a text file like this:

my_file = open("Exercises3.csv", "w+")
my_file.write("Exercise,Sets and Reps,WeightnCurls,3 x 5,25kgnBench Press,3 x 3,120kg")

This actually makes it pretty easy to convert the contents of a list, dictionary, or phrase to CSV! Likewise, we could read our files in a similar way and then just split the data by looking for commas. The main reason not to do this is that some CSV files use slightly different formatting which can cause problems opening many different files. If only you work with yours have Files, you won’t have any problems!

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 tried your first bit of JSON development and even a little bit of data science. Feel proud!

What do you do with this knowledge? Let us know in the comments below! If you’re looking to learn more skills like these, we recommend checking out our list of the best Python courses online. There you can continue your training with courses such as the Python Data Science Bundle. You can get it now for $ 37, which is a enormously Save on the usual $ 115.98!

More developer news, features, and tutorials from Android AuthorityDon’t miss to sign up for the monthly newsletter below!

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox