Reading Text Files in Python

Step-by-step Python programming tutorial

Learn the practical skills of reading data from a text file using Python, as demonstrated by a Noble Desktop instructor. Discover the process of extracting data, manipulating strings and counting word usage within a given text.

Key Insights

  • The video provides a tutorial on how to read and manipulate data from a text file using Python.
  • The presenter begins by creating a text file using data copied from a news website.
  • Python's "split" method is used to divide the text by exclamation points, resulting in a list containing a large string.
  • To further disaggregate the text, the "split" method is applied again, this time without a specified delimiter, resulting in a list of individual words.
  • The presenter demonstrates how to count the instances of a specific word in the text by converting it to lowercase and using a loop to update a counter.
  • The video highlights the flexibility of Python, allowing users to read, split, modify, and analyze strings from a text file.

Common Python Libraries

  • requests

    HTTP for humans — make API calls in two lines.

  • pandas

    DataFrames for data analysis — Excel-meets-SQL in code.

  • numpy

    Numerical computing — fast arrays and math.

  • FastAPI / Flask

    Web framework choices — FastAPI for modern async APIs.

Play video

Video Transcription

Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how you can read data from a text file.

We don't have a text file that we can read data from, so let's create one. I'm going to copy some text from CNN.com and write the data into a text file. Now, that's my text file.

I'm going to read data from that file. This data comes as a plain vanilla string. Let's assign it to a variable name, "data", and use type on "data". That's a string.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Now, I want to get rid of all the "Monday, give me a call next Monday" stuff. To do that, I'm going to use the "split" method. It's a little bit confusing because "split" comes from a string, but always returns a list.

I'm going to split it by all the exclamation points and assign it to a variable name, "list". I'm going to run "len" on that list and now I have one huge string within that list. I'm going to assign it to a variable name "string".

Now, suppose I want to go word by word. I'm going to split "string" again and leave it blank. Now, I have many words. I'm going to call it "words".

Maybe I want to count how many times they use the word "there". I could convert it to lowercase and create a counter. I'm going to set it to 0 and loop through "words". If I find the word "there", I'm going to update the counter.

Counter would be 9, so apparently they use "there" 9 times in that text.

The main idea behind this exercise is that you can use "open" to read data from a text file and then you can do whatever you like with the string. You can split it, apply lower or upper, etc. That's a string. All right, thank you.

Topics

Learn more in these courses

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram