Skip to content

Instantly share code, notes, and snippets.

@chadsconway
Last active November 22, 2020 19:44
Show Gist options
  • Save chadsconway/d618c8fff9514944db526c84f28994ff to your computer and use it in GitHub Desktop.
Save chadsconway/d618c8fff9514944db526c84f28994ff to your computer and use it in GitHub Desktop.
PYTHON-files-read-write

Using the JSON Format

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)
# load additional module
import pickle
# define a list of places
placesList = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open('listfile.data', 'wb') as filehandle:
# store the data as binary data stream
pickle.dump(placesList, filehandle)
# load additional module
import pickle
with open('listfile.data', 'rb') as filehandle:
# read the data as binary data stream
placesList = pickle.load(filehandle)
# define an empty list
places = []
# open file and read the content in a list
with open('listfile.txt', 'r') as filehandle:
for line in filehandle:
# remove linebreak which is the last character of the string
currentPlace = line[:-1]
# add item to the list
places.append(currentPlace)
# define empty list
places = []
# open file and read the content in a list
with open('listfile.txt', 'r') as filehandle:
filecontents = filehandle.readlines()
for line in filecontents:
# remove linebreak which is the last character of the string
current_place = line[:-1]
# add item to the list
places.append(current_place)
# define empty list
places = []
# open file and read the content in a list
with open('listfile.txt', 'r') as filehandle:
places = [current_place.rstrip() for current_place in filehandle.readlines()]

Using the pickle Module

  • The different methods explained up to now store the list in a way that humans can still read it. In case this is not needed the pickle module may become quite handy for you. Its dump() method stores the list efficiently as a binary data stream. Firstly, in line 7 (in the code below) the output file listfile.data is opened for binary writing ("wb"). Secondly, in line 9 the list is stored in the opened file using the dump() method.
# define list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open('listfile.txt', 'w') as filehandle:
for listitem in places:
filehandle.write('%s\n' % listitem)
# define list of places
places_list = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open('listfile.txt', 'w') as filehandle:
filehandle.writelines("%s\n" % place for place in places_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment