How to Read a Text File and Keep Only the Relevant Lines with Python

How to read a text file line by line and keep only the relevant lines using Python.

José Fernando Costa
Python in Plain English

--

Cover image
Cover image (source)

Say we have a file of English words, like the one provided in this helpful GitHub repository

aam
aani
aardvark
aardvarks
aardwolf
aardwolves
aargh
aaron
aaronic
aaronical
aaronite
aaronitic
aarrgh
aarrghh

but we only want to keep the 5-letter words. List comprehensions simplify this task:

with open("some_file.txt", "r") as f:
words = [line.strip() for line in f if len(line.strip()) == 5]

Three things to note here:

  • The file is opened in “r”ead-only mode via the open function in a context manager, i.e. a with block
  • Line by line processing is achieved by iterating over the file object generated by open, named fhere
  • An if clause is included in the list comprehension to ensure only 5-letter words are stored in the words list — note the use of strip to remove any whitespace in the text line

Bonus random tip: save the output as a JavaScript file

What if you wanted to then save this list as an array variable for a JavaScript file? That’s easy too.

with open("a_new_javascript_file.js", "w") as f:
f.write(f"wordArray = {words}")
  • The second parameter of open is “w” because we want a file object that can be written to.
  • The generated file contains a single variable, wordArray, which is identical to the words Python list — the string used to write the file contains this variable assignment.
  • Note the f prefixed to the string stands for f-strings, not the file object f.

And there you have it. Thank you for reading.

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

--

--