3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JSONの読み込み方

Last updated at Posted at 2016-09-18

JSONの読み込み方

まずは普通にファイルを読み込む

with open('test.json', 'r') as f:
	js=f.read()

jsonとしてloadするように指示すると

import json
nb=json.loads(js)

ディクショナリ形式として扱いやすい!
外部パラメータ格納に使えるね

print(nb)
# [Out]#{'color_scheme': 'Packages/User/Tubnil_kai (SL) (SublimePythonIDE).tmTheme', 'enable_tab_scrolling': True, 'detect_slow_plugins': False, 'always_show_minimap_viewport': True, 'draw_white_space': 'all', 'draw_minimap_border': True, 'file_exclude_patterns': ['*.exe', '*.zip', '*.lnk', '*.db', '*.pptm', '*.docx', '*.pdf', '*.dwt', '*.bak', '*.xlsx', '*.ex4', '*.ex4old']}

print(nb['color_scheme'])
# [Out]#Packages/User/Tubnil_kai (SL) (SublimePythonIDE).tmTheme

print("*.exe" in nb['file_exclude_patterns'])
# [Out]#True

json.loads()じゃなくてjson.load()ならばファイルから直接読めるみたい
短くできる!

import json
with open('test.json', 'r') as f:
    nb=json.load(f)
print(nb)
3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?