jsonを扱う

import urllib.request as req
import os.path, random
import json

url = "hoge"
savename = "hogehoge"
if not os.path.exists(url):
	req.urlretrieve(url, savename)

data = json.load(open(savename, "r", encoding="utf-8"))

r = random.choice(data)
print(r['kami'], r['simo'])

jsonを作る。

import json

price = {
	"date": "2018-08-11",
	"price": {
			"apple": 80,
			"orange": 55,
			"banana": 40
	}
}
s = json.dumps(price)
print(s)

[vagrant@localhost python]$ python3 app.py
{“price”: {“apple”: 80, “orange”: 55, “banana”: 40}, “date”: “2018-08-11”}