Last active
August 29, 2015 14:09
-
-
Save kkAyataka/bf5f4e76268f48abe9c0 to your computer and use it in GitHub Desktop.
Python load json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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