The binary data format pickle uses is specific to Python. To improve the interoperability between different programs the JavaScript Object Notation (JSON) provides an easy-to-use and human-readable schema, and thus became very popular.
The following example demonstrates how to write a list of mixed variable types to an output file using the json module. In line 4 the basic list is defined. Having opened the output file for writing in line 7, the dump() method stores the basic list in the file using the JSON notation.
import json
# define list with values
basicList = [1, "Cape Town", 4.6]
# open output file for writing
with open('listfile.txt', 'w') as filehandle:
json.dump(basicList, filehandle)
Reading the contents of the output file back into memory is as simple as writing the data. The corresponding method to dump() is named load(), and works as follows:
import json
# open output file for reading
with open('listfile.txt', 'r') as filehandle:
basicList = json.load(filehandle)