Storing data in a file

Download python eBook

Example

The following snippet encodes the data stored in d into JSON and stores it in a file (replace filename with the actual name of the file).

import json

d = {
    'foo': 'bar',
    'alice': 1,
    'wonderland': [1, 2, 3]
}

with open(filename, 'w') as f:
    json.dump(d, f)

Stats

Contributors: 1
2016-03-27
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook