Hey! It’s Roberts Greibers here.

I’ll share details about my 7+ years of experience as a Python developer with you a bit later in the post (you’ll have to scroll down to find them).

But now, let’s talk about how I came to write my “read from CSV Python example” script.. πŸš€

Firstly, understand that CSV is a file format supported by nearly all platforms..

That’s why for Python programming purposes I’d suggest you use CSV format instead of any other excel format.Β 

It’s possible to find a Python excel library and use Python for excel files in general, but in this post, we’re going to strictly focus on how to read CSV file in Python.Β 

Also, we’re going to check:

  • Should you learn Excel or Python? (using Python in Excel?)
  • Are there any ways you can use Python for excel? (e.g. Parsing CSV Python)
  • Any Python excel library you could find and use? (e.g. Python CSV file reader)
  • How to import CSV file in Python? (the process of importing CSV into Python)

If you’ve been Googling for “Python Read CSV” or the typical “Reading CSV file in Python” you’ve found the RIGHT place! πŸŽ‰

I’m about to explain the details of how I optimize my time by parsing spreadsheets in Python (e.g. CSV in Python) and how I avoid manual CSV reading tasks using Python.Β 

Building a quick CSV file reader in Python can take you up to 30 minutes and save you hours later assuming you’re using the script on a daily basis.Β 

Let’s get into it and I’ll also share a way that helped me and might even help you to become a professional Python developer at the end of the post πŸ‘‡πŸ»

Should You Learn Excel Or Python?

Having the knowledge of reading a CSV file in Python versus reading a CSV file just by opening it in excel has a large difference in your career. 

Of course, it depends on the career path you’ve chosen, but when it comes to software development no one is going to take you seriously if you’re going to list “reading Excel files but not with a Python excel library” as one of your skills.

While on the other side, if you’d say: 

  • I’ve got extensive knowledge when it comes to writing so-called “read from CSV Python scripts” or “read CSV Python applications”
  • I’ve worked for several companies and in all of them we were parsing CSV files in Python
  • I’ve worked with one of the most used Python excel library called csv, etc.Β 

You get the point.. 

It totally changes the way someone looks at you and HOW EXPERIENCED YOU SEEM TO APPEAR! πŸ”₯

Python Read CSV Accounting Software

Another good reason for learning to read CSV file in Python is just pure fact that most tech companies will always be involved in some sort of accounting.

Take for example the tech company I work for as a Python/Django backend developer https://ourspell.com/ (more details on my Linkedin)

Reading CSV File In Python - The situation in real companies!
Reading CSV File In Python – The situation in real companies!

We’re not strictly an accounting company, we’re building a payment gateway platform, but naturally, tax calculation, invoices, etc. all of them are always handled with CSV parsing or with  CSV write in Python. 

I’m planning to write another post soon on how to develop Python CSV writer, browse around the blog and you’ll be able to find it soon if I haven’t linked it somewhere here already. πŸ‘ŒπŸ»

Reading CSV File In Python To Practice Coding

Reading CSV file in Python just to practice your coding skills is a good start, but it won’t get you far. 

You need tangible Python projects in your portfolio! πŸ”₯

One of my Python Mentoring graduates Yuliia first came to me with just a couple of Python read CSV (Python Excel) scripts. (read the whole Yuliia story here)

Here’s a part of the initial email conversation we had (this is before Yuliia joined Python Mentoring):

Before Yuliia joined Python Mentoring she only had a couple of "Python read CSV" scripts
Before Yuliia joined Python Mentoring she only had a couple of “Python read CSV” scripts

Now months later after getting hired and already working as a Python developer Yuliia reached out to me just to chat..

She’s already planning her next move, getting hired at Spotify or Hulu..

Anyways, here’s what she noticed while just starting to look for better software developer positions..

WHAT COMPANIES ARE ACTUALLY LOOKING FOR! πŸ‘‡πŸ»

Hearing back from Yuliia, just to see how far she made it makes me happy!

If you’re in a similar situation and also willing to work hard for your career…

see if there’s any way you can contact me at the end of this post!

I’m willing to talk to you and see if I can help you the same way I helped Yuliia!

🚨 In Python Mentoring we tend to focus on preparing you for a real Python developer career, teaching you only the stuff that’s actually used in the industry by large tech companies!

Importing CSV Files In Python (How I Did It)

Just to give you a very simple Python read CSV example, let’s take the process of keyword research of this blog post.

For good keyword research, I need to manually open and read CSV files. I need to collect keywords with higher estimated page visits and somehow implement them into my blog posts.

Naturally, you have to rank keywords by estimated visits from a bunch of CSV files. 

This is such a repetitive task I couldn’t handle it for more than 2 blog posts so I wrote a Python read CSV script for it!

The above csv_files.zip file contains just a few CSV files so you can follow along with the Python code I’m about share to read CSV files.

Here’s an example of the content of these CSV files:

NoVolumePositionEstimated VisitsSearch Difficulty
1python read csv1806361
2read from csv python1709664
3reading csv file in python1612352
4read csv file in python1415051
5parsing csv python2203549
6csv file reader in python1189253
7csv read1178948
8csv file read in python2167147
9read csv file python1150361
Reading CSV file in Python (example of CSV file content)

Read From CSV Python Code Example

When it comes to reading CSV files in Python, you have to consider three major steps. πŸ‘‡πŸ»

Firstly, you need to figure out how to open CSV files in Python.

Secondly, you need to understand how you can read CSV file in Python (how to actually collect information from each CSV file)

And finally, you need to know how to do the necessary CSV parsing in Python in order to create the summary of CSV file content. 

How To Open CSV Files In Python

Before reading a CSV file in Python, you have to figure out how to define PATH to the CSV file. 

In this case, I’ve used glob.glob() in a combination with * to select all CSV files of a particular directory recursively.

import glob
import typing

ROOT_PATH: str = (
    '.'
)

KEYWORDS_DIR: str = 'csv_files'

CSV_FILES: typing.List[str] = glob.glob(f'{ROOT_PATH}/{KEYWORDS_DIR}/*')

collection = {}

for csv_file_index, csv_file_path in enumerate(CSV_FILES):
    with open(csv_file_path) as csv_file:
        print(csv_file)

Ignore enumerate() for now, it’s a necessary part of the following Python code block.

The important part for opening a CSV file is open()

Python Open CSV File For Reading
Python Open CSV File For Reading

Use open() built-in function to simply set up a file context in Python where as csv_file gives you the access to the file.

Output:

<_io.TextIOWrapper name='./csv_files/4.csv' mode='r' encoding='UTF-8'>
<_io.TextIOWrapper name='./csv_files/1.csv' mode='r' encoding='UTF-8'>
<_io.TextIOWrapper name='./csv_files/3.csv' mode='r' encoding='UTF-8'>
<_io.TextIOWrapper name='./csv_files/2.csv' mode='r' encoding='UTF-8'>

The above output is a result of print(csv_file) you can clearly see how CSV files are opened in Python as objects that you can access.

Reading CSV File In Python

In order to read a CSV file in Python you’ll need to apply csv.reader() function with a delimiter.

Python CSV Reader Function
Python CSV Reader Function

Python CSV Reader documentation might suggest other ways, but what actually worked for me, in this case, was using the following line:

csv_reader = csv.reader(csv_file, delimiter=',')

Once you have the access to the CSV file content, you want to iterate over each row and start to think about which data is available in which row, here’s an example of exactly that.

if csv_file_row_index == 0:
    continue

Notice, I’ve skipped the first row as it is empty. 

import csv
import glob
import typing

ROOT_PATH: str = (
    '.'
)

KEYWORDS_DIR: str = 'csv_files'

CSV_FILES: typing.List[str] = glob.glob(f'{ROOT_PATH}/{KEYWORDS_DIR}/*')

collection = {}

for csv_file_index, csv_file_path in enumerate(CSV_FILES):

    with open(csv_file_path) as csv_file:

        csv_reader = csv.reader(csv_file, delimiter=',')

        for csv_file_row_index, row in enumerate(csv_reader):

            if csv_file_row_index == 0:
                continue

            no: str = row[0]
            keyword: str = row[1]
            estimated_visits: str = row[4]

            print('no: ', no)
            print('keyword: ', keyword)
            print('estimated_visits: ', estimated_visits)
            breakpoint()

Just before we go over any other rows in a CSV file, I’d suggest using a breakpoint(), stopping for a second and figuring out if you have accessed the correct rows. 

In my case, I’m interested in reading keyword and estimated_visits rows the most. 

So, here’s a print of those values from the first CSV file. 

Output:

no:  1
keyword:  csv file read in python
estimated_visits:  144
> /Users/robertsgreibers/projects/pythonic.me/8_python_read_csv/2.py(19)<module>()
-> for csv_file_row_index, row in enumerate(csv_reader):
(Pdb) 

Using CSV File Content With Python

Building on top of the above Python code blocks we’re at the point where you can decide what to do with the data from each CSV file you have read. 

I personally want to collect only the first 10 rows of each file, that’s why the following if statement is used:

if int(no) > 10:
    continue

Then, my plan is to collect each keyword and sum estimated visits in collection dictionary. 

import glob
import typing
import csv
import json


ROOT_PATH: str = (
    '.'
)

KEYWORDS_DIR: str = 'csv_files'

CSV_FILES: typing.List[str] = glob.glob(f'{ROOT_PATH}/{KEYWORDS_DIR}/*')


collection = {}

for csv_file_index, csv_file_path in enumerate(CSV_FILES):

    with open(csv_file_path) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')

        for csv_file_row_index, row in enumerate(csv_reader):

            if csv_file_row_index == 0:
                continue

            no: str = row[0]
            keyword: str = row[1]
            estimated_visits: str = row[4]

            if int(no) > 10:
                continue

            _e = 'estimated_visits'

            if collection.get(keyword):

                __e = collection[keyword]

                collection[keyword] = __e + int(estimated_visits)
            else:
                collection[keyword] = int(estimated_visits)


sorted_by_val = {
    k: b for k, b in 
    sorted(collection.items(), key=lambda element: element[1], reverse=True)
}

print(' ')
print('SUMMARY:')
print(json.dumps(sorted_by_val, indent=4))

And in the end, I’m not really interested to save this information anywhere else besides my notes..

Before you print a summary of all the keywords found, you’d want to order them by estimated visits.

This is the section of code that does exactly that:

sorted_by_val = {
    k: b for k, b in 
    sorted(collection.items(), key=lambda element: element[1], reverse=True)
}

You’d want to see keywords with higher estimated visits at the top of the summary, here’s the code for that.

And finally, if you’d print the result, this is the prettified end result.

SUMMARY:
{
    "csv": 6123,
    "read from csv python": 5074,
    "python read csv": 5019,
    "csv write in python": 4508,
    "python csv writer": 4508,
    "csv write python": 4508,
    "python csv writerows": 4508,
    "csv writerows python": 4508,
    "python csv writerow": 4508,
    "python write to csv": 4508,
    "importing csv into python": 831,
    "import csv file in python": 243,
    "import csv file into python": 243,
    "importing csv files in python": 243,
    "import csv pandas": 151,
    "csv file read in python": 144,
    "read csv file in python": 144,
    "reading csv file in python": 144,
    "csv file reader in python": 144,
    "how to import csv file in python": 129,
    "import csv python": 109,
    "python csv": 84,
    "import excel into python": 76,
    "importing csv": 75,
    "how to import csv into python": 70,
    "import csv python pandas": 69,
    "import excel file in python": 69,
    "import excel file into python": 69,
    "import excel files in python": 69,
    "python import excel": 67,
    "import xlsx into python": 59,
    "import excel file python": 59,
    "python excel": 53,
    "pd read excel": 48,
    "python read csv file": 43,
    "pandas read excel": 36,
    "python csv file read": 35,
    "read csv file python": 30,
    "python csv file reader": 30
}

Why It’s HARD For You To Write Your Own “Python Read CSV” Script?

If you struggle to read CSV file in Python (develop your own Python scripts) it doesn’t mean there’s something wrong with you..

In fact, I’d say everyone will struggle in the beginning..

It doesn’t matter HOW SMART you think you are..

Programming is a really difficult skill to master! 🐍

The Process Of Writing A CSV File Reader In Python

You could be doing everything correctly, listening to Youtube videos, following instructions in Python blog tutorials..

🚨 But writing a simple Python script will still take you a lot of TIME AND ENERGY!

WHY?

Well, here’s the reality..

Most people think they can become Python developers on their own, for example, “reading CSV file in Python” information is available online anyways, right?

WRONG!

As someone who’s learning Python, you’ll be making mistakes and that’s okay…

As a beginner, you’ll be constantly making mistakes, making bad decisions, guessing and kind of blindly doing something that doesn’t even bring you closer to becoming a Python developer!

Leaving a code review comment for a Python Mentoring student.
Leaving a code review comment for a Python Mentoring student.

⚠️ You really don’t have anyone to give you A SYSTEM to follow..

I used to be in this situation and most of my students have been in this situation – that’s the usual case for Python beginners!

My Python Excel Backstory

For you, the best approach is to find people you can LEARN from.. πŸ”₯

Soak up all the knowledge they have gathered and just implement it into your career!

Literally, copy HOW THEY WORK and what works for them!

If you’d just go and try to figure out everything on your own you’ll get LOST in the ocean of information that’s available online for Python developers!

I used to be a Quality Assurance Engineer (more on my Linkedin experience section)

My quality assurance experience from years ago..
My quality assurance experience from years ago..

For a long time I dabbled around with simple Python scripts, e.g. building CSV file reader type of scripts in Python 🐍

They can be useful in certain situations, but the skill to develop a Python Excel script is NOT what will get you hired as a Python developer!

I wish that back in the day, someone had told me WHAT TO DO, and what steps to take, it would have saved me so much time!

Yet, I was keen on “figuring out” everything by myself..

Don’t be like me, find someone who has already figured out the steps and help you!

Why You MUST Surround Yourself With Python Professionals

Only when I surrounded myself with way more experienced Python developers I learned the necessary tools and programming concepts..

Literally the SECRET knowledge no one ever explains in Youtube videos, blog posts, etc.

Especially when it comes to coding bootcamps, THEY SHOULD BE teaching exactly the tools and concepts used in the industry…

But what I find is most coding bootcamp graduates don’t even know how to debug and fix small bugs, let alone create a simple website project.. πŸ€¦πŸ»β€β™‚οΈ

Python Read CSV - Free Consultation Call

If you can find my contact info below this post (somewhere before the comments section), there’s a chance you can still hop on a call with me..

⚠️ I’m open to discussing your experience and seeing if there’s a chance I can help you become a Python developer…

I know from my own experience, as a beginner, there’s no way you’ll stumble upon the required information online.

You have to find people who are already in the industry..

You have to HOPE these people will be willing to help you.. 

That’s the purpose of our call, please don’t waste my time if you’re not serious about your career!

If you don’t hear back from me, there’s a high chance I saw your message and just decided to ignore it based on how you have presented yourself.

First impression matters!

Why My Students Are NOT Building Python Read CSV Scripts

Although I have nothing against “reading CSV file in Python” scripts it’s NOT a skill required to get you hired as a Python developer!

In my Python mentorship, students have the chance to talk to me over a Zoom Call every week!

In those calls, I answer all student questions and share my thought process behind solving development problems!

On top of that, I share my own FRAMEWORKS and CHEAT SHEETS for Python & Django with students..

This is HOW YOU LEARN!

You take knowledge, take the mindset of someone ahead of you..

And you IMPLEMENT the same exact STRATEGY into your career! πŸ”₯

Python Read CSV - Zoom Live Call

🚨 I can only work with a couple of people at the moment as my time is VERY LIMITED, please don’t message me if you’re NOT serious about becoming a Python developer! 🚨

You have to take your life seriously!

I can only share with you what has worked for me, I’m NOT here to fix your life!

The only way you’ll be able to PROVIDE for your family and make everyone else’s life better around you is by taking the RESPONSIBILITY of CHANGING YOUR LIFE into your own hands!

Or you can just DO NOTHING…

The fact that you read this far… and decided to do nothing just once again proves how comfortable you’re with wasting your time, essentially wasting your life away!

The choice is yours!

Talk soon!

I'll help you become a Python developer!

If you're interested in learning Python and getting a job as a Python developer, send me an email to roberts.greibers@gmail.com and I'll see if I can help you.

Roberts Greibers

Roberts Greibers

I help engineers to become backend Python/Django developers so they can increase their income