Home > Uncategorized > Data serialization in python using Json.

Data serialization in python using Json.

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.

Advertisements
Categories: Uncategorized Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: