• Active Topics 

Reading Text File Lines into Python

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

We can make a simple python code to read plain text file lines and look for patterns, and probably get some statistics on those patterns.

Suppose we have a very large text file, and each line contains either ON/ OFF, ON and OFF or some other text that starts with ON/ OFF, and we are only interested to know the number of ON's and OFF's contained in the text file.

Let's find out how we can do that.

Download the attached "on_off.txt" plain text file and put it in the same directory as the program below:
  1. """Parsing TextFiles in Python"""
  2. #Open and read file
  3. with open("on_off.txt") as f:
  4.     lines = f.readlines()
  5.     lines = [x.strip('\n') for x in lines] #Optional
  6.     print(lines)
  7.     #Look for patterns
  8.     countON = 0
  9.     countOFF = 0
  10. for line in lines:
  11.     line = line.strip().upper() #Remove white spaces
  12.     if line.find("ON")!= -1 and len(line)==2:
  13.         countON += 1
  14.     if line.find("OFF")!= -1 and len(line)==3:
  15.         countOFF += 1
  16. print("ON:", countON)
  17. print("OFF:", countOFF)

and then run the program as you would normally do for any Python code. What do you see?

Here is an explanation on what the code does:
  • "with open" functions create a file object and store it in the variable "f"
  • f.readlines() method reads all the lines in a file and returns them as a Python list
  • countON and countOFF are initializations for ON and OFF counts
  • The for loop processes each line separately, "line.strip()" removes/strips white spaces before and after each text of our interest, in this case ON and OFF, upper() function transforms a text into uppercase
  • We check the length of each text by "len()" to make sure that the text of our interest is of intended length and not embedded into anything else
Python uses several file modes depending on what you want to do with the file. When you use open() function, you can optionally specify a file mode , the common mode is "r" for read and "w" to write into a file. You can specify these modes as follows:

Open a file and read its contents

Code: Select all

file = open("on_off.txt", "r")
or open a file and write into it

Code: Select all

file = open("on_off.txt", "w")
Attachments
on_off.txt
(131 Bytes) Downloaded 386 times
on_off.txt
(131 Bytes) Downloaded 386 times
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests