Lesson 18Processing Text Files
In many cases, when we need to analyze the contents of text for a specific purpose, the text already exists in a file, so it can be efficient to have Python read the text directly from the source file. Not only is this faster than manually inputting the text at runtime, but it also means that Python can look at a file whose contents may change over time. This allows Python to automatically perform the analysis on the most recent version of the file.
In addition, we may want to save the output of a script to use in another script or elsewhere. Python allows us to save the output to a file easily.
In this and the next two lessons, we will look at three main text file types:
- In this lesson, we'll review plain text files (
.txt
), such as the text from a book, or reviews collected by an online retailer. - In Lesson 19, we'll review CSV files (
.csv
), which include records organized into columns like a table and are often generated by structured datasets. - In Lesson 20, we'll review JSON files (
.json
), which can represent unstructured datasets with key: value pairs
We can use Python scripts to read these files, add (or append) content to an existing file, and create new files. We'll start with this lesson that covers plain text files.
Get Job Ready Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.