Reading and Writing Lists to a File in Python

In this tutorial, we will learn how we can read and write the lists in the file using Python. As we know that, Python uses the list for storing the various types of data and it is mutable in nature. To store the data persistently, we require to store either a file or database to properly work.

Python programming language provides the write() and read() methods to read and write file. These methods are most suitable for the single line. The readlines() and writelines() method used to work with multiple files.

Using the read() and write() Methods

The read() and write() methods work very effectively with the characters (string). Let's understand the following example where we write the list line by line in the file.

Example -

The listitem is extended by a line break "\n" then we stored into the output file. Now, let's read the entire list from the file newfile.txt back into the memory.

Example -

Here, we need to remove the line break from the end of the string. It helps us that Python allows list operations on the string. We can simply use the list index operator [:-1] which removes '\n' from the last.

Using the writelines() and readlines() Methods

These methods are used to write and read multiple lines in the one step, respectively. Let's write the entire list in the file.

Example -

Now read the lists from file using the readlines() method.

Example -

The above code follows the traditional approach borrowed from other programming language. Now, let's write the code in Pythonic way.

Example -

In the above code, we read the file content using the readlines() method. Secondly, in a for loop from each line the line break character is removed using the rstrip() method. Then, we added the list element to the list of capitals as a new list item.

The above code is more compatible in comparison with the listing before. However, it may be difficult to read for beginner Python programmer.

Using the Joblib Module

In the above section, we explained regular methods of the Python which stores the list element in such way that human can still read it quite literally a sequential list in a file. This is a simple way to create the easy report or outputting export files for users to further use.

But if our aim is to just serialize the list into a file, which can be loaded later. Then we don't need to store it in a human-readable format.

Python provides the joblib module which allows to dump Python object easily.

Example -

The joblib module provides the easiest way to serialize objects in an efficient format, and load from later. We can use any arbitrary format, such as .sav, .data, etc. It doesn't really matter - both joblib and alternatives like pickle will read the files just fine.

Using the Pickle Module

It is an alternative to joblib, we can use pickle module and its dump() method stores the list efficiently as a binary data stream. Firstly, the output file newfile.data is opened for binary writing ("wb"). Secondly, the list is stored in the opened file using the dump() method. Let's understand the following example.

Example -

In the next step, we read the list from the file by opening the file in reading ('rb'). Secondly, the list of the capitals loaded from the file using the load() method.

Example -

We have mentioned two examples to demonstrate the usage of strings. Though, pickle works with all kinds of Python objects such as strings, integers, and other built-in data structure in Python.

Using the Json Format

JSON stands for JavaScript Object Notation which is a popular for interoperability between different programs. It provides easy to use and human readable schema, and thus became very famous for serializing files and sharing them over APIs.

Let's understand the following example.

Example -

In the above code, we have initialized a list of the various types of the element to an output using the json module. We opened the file in the writing mode then store the list in the in JSON notation using the dump() method.

Now, let's read the JSON data in the following example.

Example -

Conclusion

This tutorial included the various methods to reading/writing data and we have also discussed dumping/loading data via binary stream using pickle and JSON. This simplifies storing a list persistently and reading it back into memory.






Latest Courses