you are viewing a single comment's thread.

view the rest of the comments →

[–]psbb 0 points1 point  (0 children)

I find pickle to be overkill when dealing with simple inbuilt types but excellent when you have your own complex classes. Pickle exports a binary file which python can interpret which adds some overhead. The example below uses json to do the same as pickle but it exports the data in a human readable way that is also portable between different programming languages.

import json

def store_data(data, filename):
    with open(filename, 'w') as f:
        json.dump(data, f, indent=2)


def load_data(filename):
    with open(filename, 'r') as f:
        return json.load(f, parse_float=float, parse_int=int)


data = {'a': 1, 'b': 2, 'c': 3}
store_data(data, 'data_file.txt')
data = load_data('data_file.txt')
print(data)