Archive

Posts Tagged ‘json’

Data serialization in python using Json.

September 7, 2010 Leave a comment

Serialization is helpful when we have many variables to store in the file (in example games) or we need to move it beetween programming languages.

JsonĀ  is text base serialization system, we can store (dump) data in-file and read it back. We need to remamber order of storing elements.

(Module named json is available in python 2.6 or greater)

#!/usr/bin/python
import json
basic_entry=['data',123.45,12345,{'key':10}]
with open('basic.json', mode='w') as f:
 json.dump(basic_entry, f,indent=2)

with open('basic.json', mode='r') as f:
 loaded=json.load(f)
 print loaded

We got a file named basic.json


[
 "data",
 123.45,
 12345,
 {
 "key": 10
 }
]

Its very simple and flexible way to store many data in a file.

Categories: Uncategorized Tags: ,