Skip to content

Instantly share code, notes, and snippets.

@kkAyataka
Last active August 29, 2015 14:09
Show Gist options
  • Save kkAyataka/bf5f4e76268f48abe9c0 to your computer and use it in GitHub Desktop.
Save kkAyataka/bf5f4e76268f48abe9c0 to your computer and use it in GitHub Desktop.
Python load json file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""jsonファイル読み込み"""
import json
# ファイルから読み込み
# jsのobjectはpythonのdictとして読み込まれる
# https://docs.python.org/2.7/library/json.html?highlight=json#json-to-py-table
f = open('client_secret.json', 'r') # ファイルが無いとIOError
csfs = json.load(f)
# stringから読み込み時はloads
#csfs = json.loads(f.read())
try:
cid = csfs['installed']['client_id'] # Keyが無いとKeyError
cs = csfs['installed']['client_secret']
print cid
print cs
except KeyError as e:
print '"{0}" is not found'.format(e.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment