Categories
python starter projects

Super Simple Python: Reading and Writing to a Text File

Reading and writing to a text file are two of the most basic ways to import and export data with Python. In 2022, Python is strongly known for machine learning use cases. While text files are not traditionally thought of as ML data containers, they can often be the medium of inference. For example, I just created and monetized a project with The Text API which reads in and analyzes .txt files!

As we usually do in the Super Simple Python series, we’re going to cover the simplest way to write and read from a text file in Python. In this post we’ll cover:

  • How to Write Text to a Text File
  • How to Read Text from a Text File
  • Testing Reading and Writing from a Text File in Python
  • Nuances of Text Files – UTF-8, ASCII, and Unicode
  • Summary of Reading and Writing to a Text File in Python

How to Write to a Text File

Our super simple Python example creates two functions, a write and a read. We will cover the writing first. Let’s create a function called write_text that takes one parameter, a filename. The filename is the name of the file we save the input to.

The first thing we do in the function is ask the user for an input. We save that input and write it to an open file. Note that we can also use two parameters and pass in text to save. In this case, I opted to ask the user for a more interactive experience.

def write_text(filename):
   text = input("What would you write to a text file? ")
   with open(filename, "w") as f:
       f.write(text)

How to Read from a Text File with Python

Reading text from a text file is just as easy as writing to one. We create a read_text function that takes just one parameter, the filename. This filename is the name of the file that we will read the text from. All we do is open up the file, read() the text into a variable, and return the variable.

def read_text(filename):
   with open(filename, "r") as f:
       text = f.read()
   return text

Testing Python Reading and Writing Text Functions

Finally, let’s test out our text reading and writing functions. We first call the write function with the filename test.txt and then call the read function with the exact same filename. The write function will prompt us to input text for the text file. Then, the read function should print that exact same text back to us.

write_text("test.txt")
print(read_text("test.txt"))

For my test I used the sentence: “Hi, my name is Yujian Tang, I’m the founder and CEO of The Text API, the creator and maintainer of PythonAlgos, and one of the best software tutorial creators alive.” The test should look like this:

Nuances of Text Files – UTF-8, ASCII, and Unicode

Before we wrap up here, let’s look at some of the nuances of text files. There are many different types of encodings, but for now we will look at the most popular types. UTF-8 is the most popular on the web. It stands for “Unicode Transformation Format”. It is a variable width character encoding that uses between one and four eight-bit bytes to store unicode characters.

Okay, so UTF-8 is for Unicode characters and uses eight-bit bytes. What about UTF-16? UTF-16 is another unicode transformation format that uses between one and two 16-bit code units to store each character. It is capable of encoding all 1,112,064 valid code points in unicode. UTF-16 is particularly popular with Windows machines. Especially due to the makeup of the Windows API, Java, and JavaScript. 

Finally, let’s look at ASCII. UTF-16 is actually the only popularly known encoding that is incompatible with ASCII. Meanwhile, UTF-8 includes ASCII values in its character set. ASCII stands for “American Standard Code for Information Exchange”. You can see why UTF-8 is more popular, it allows for other languages as well. ASCII contains a standard 128 character-set for alphanumeric characters, special characters, and specific commands.

The main thing to be aware of with these encodings is which one you’re using when writing and reading text. Python defaults to UTF-8. If you want other character encodings you need to use the encoding=”<charset>" option inside of your read or write.

Summary of Reading and Writing to a Text File in Python

In this post we learned about how to create two simple functions. One to write some text to a txt file and one to read from a txt file. We also learned a bit about encodings. The encodings we looked at are UTF-8, UTF-16, and ASCII. UTF-8 is the most popular encoding and allows for ASCII values. UTF-16 is popular in Microsoft products such as the Windows API and Word, but does not work with ASCII. If you want custom encodings in your text, you need to pass the encoding option to your reads and writes.

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Leave a ReplyCancel reply