Monday, June 24, 2013

Python: Reading a JSON dataset

I just completed an online Python course.  I have been experimenting and there is much about this language to love!

Here is a simple JSON dataset, called min_max_json.txt:

{
"min": 34,
"max": 82
}


Here is the source code, called read_min_max_json.py, which reads the JSON dataset and calculates the range (defined as max - min):

import json
data = json.loads(open('min_max_json.txt').read())
print("This is what was read in:")
print(data)
min = data["min"]
max = data["max"]
range = max - min
print("The range is " + str(range) + ".")


Here is the output:

This is what was read in:
{'min': 34, 'max': 82}
The range is 48.


Note: Using Python 3.3.2 with IDLE.


No comments:

Post a Comment