質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

3回答

602閲覧

pythonの辞書型から特定の値を取る

sizumita

総合スコア196

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/08/24 05:40

###前提・実現したいこと
pythonで、JSONファイルから辞書型をを作り、その中から値を取り出して、URLにつなげることをやりたいと思っています。JSONファイルから取り出すことはできたのですが、そこから特定の値が入った物だけを別の関数に入れるやり方がわかりません。URLはできました。

###該当のソースコード

python

1import json 2 3 4f = open('url.json','r') 5json_dict = json.load(f) 6print(format(json_dict['items']))

json

1 2items = [ 3 { 4 "kind": "books#volume", 5 "id": "ldSFRJaVbxAC", 6 "etag": "sjGC/Bii148", 7 "selfLink": "https://www.googleapis.com/books/v1/volumes/ldSFRJaVbxAC", 8 "volumeInfo": { 9 "title": "On the Job", 10 "subtitle": "Confronting the Labour Process in Canada", 11 "authors": [ 12 "Craig Heron", 13 "Robert H. Storey" 14 ], 15 "publisher": "McGill-Queen's Press - MQUP", 16 "publishedDate": "1986-01-01", 17 "description": "Every day millions of Canadians go out to work. They labour in factories, offices, restaurants, and retail stores, on ships, and deep in mines. And every day millions of other Canadians, mostly women, begin work in their homes, performing the many tasks that ensure the well-being of their families and ultimately, the reproduction of the paid labour force. Yet, for all its undoubted importance, there has been remarkably little systematic research into the past and present dynamics of the world of work in Canada.", 18 "industryIdentifiers": [ 19 { 20 "type": "ISBN_10", 21 "identifier": "0773505997" 22 }, 23 { 24 "type": "ISBN_13", 25 "identifier": "9780773505995" 26 } 27 ], 28 "readingModes": { 29 "text": false, 30 "image": true 31 }, 32 "pageCount": 360, 33 "printType": "BOOK", 34 "categories": [ 35 "Law" 36 ], 37 "maturityRating": "NOT_MATURE", 38 "allowAnonLogging": false, 39 "contentVersion": "preview-1.0.0", 40 "imageLinks": { 41 "smallThumbnail": "http://books.google.com/books/content?id=ldSFRJaVbxAC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api", 42 "thumbnail": "http://books.google.com/books/content?id=ldSFRJaVbxAC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" 43 }, 44 "language": "en", 45 "previewLink": "http://books.google.co.jp/books?id=ldSFRJaVbxAC&printsec=frontcover&dq=job&hl=&cd=1&source=gbs_api", 46 "infoLink": "http://books.google.co.jp/books?id=ldSFRJaVbxAC&dq=job&hl=&source=gbs_api", 47 "canonicalVolumeLink": "https://books.google.com/books/about/On_the_Job.html?hl=&id=ldSFRJaVbxAC" 48 }, 49 "saleInfo": { 50 "country": "JP", 51 "saleability": "NOT_FOR_SALE", 52 "isEbook": false 53 }, 54 "accessInfo": { 55 "country": "JP", 56 "viewability": "PARTIAL", 57 "embeddable": true, 58 "publicDomain": false, 59 "textToSpeechPermission": "ALLOWED", 60 "epub": { 61 "isAvailable": false 62 }, 63 "pdf": { 64 "isAvailable": true, 65 "acsTokenLink": "http://books.google.co.jp/books/download/On_the_Job-sample-pdf.acsm?id=ldSFRJaVbxAC&format=pdf&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api" 66 }, 67 "webReaderLink": "http://play.google.com/books/reader?id=ldSFRJaVbxAC&hl=&printsec=frontcover&source=gbs_api", 68 "accessViewStatus": "SAMPLE", 69 "quoteSharingAllowed": false 70 }, 71 "searchInfo": { 72 "textSnippet": "Yet, for all its undoubted importance, there has been remarkably little systematic research into the past and present dynamics of the world of work in Canada." 73 } 74 ]

json

1"industryIdentifiers": [ 2 { 3 "type": "ISBN_10", 4 "identifier": "0773505997" 5 }, 6 { 7 "type": "ISBN_13", 8 "identifier": "9780773505995" 9 } 10 ]

この"industryIdentifiers"の、"identifier": "0773505997"の値を撮りたいです。

###試したこと
辞書型の中身を取るやり方をやって見ましたが、うまくいきませんでした。

###補足情報(言語/FW/ツール等のバージョンなど)
python3.5.1

ご教授願います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

ベストアンサー

いまひとつ提示されているjsonデータ構造および欲しい値が把握できていませんが、以下のような感じでしょうか。
不要な値はif文なりで飛ばしてください。

Python

1s = """ 2{ 3 "hoge" : "huga", 4 "items" : [ 5 { 6 "volumeInfo" : { 7 "industryIdentifiers" : [ 8 { 9 "type" : "ISBN_10", 10 "identifier" : "0773505997" 11 }, 12 { 13 "type" : "ISBN_13", 14 "identifier" : "0773509999" 15 } 16 ] 17 } 18 }, 19 { 20 "volumeInfo" : { 21 "industryIdentifiers" : [ 22 { 23 "type" : "ISBN_20", 24 "identifier" : "1111" 25 }, 26 { 27 "type" : "ISBN_23", 28 "identifier" : "2222" 29 } 30 ] 31 } 32 } 33 ] 34} 35""" 36import json 37json_dict = json.loads(s) 38 39# 先頭のひとつだけ 40items = json_dict['items'] 41print(items[0]['volumeInfo']['industryIdentifiers'][0]['type']) 42print(items[0]['volumeInfo']['industryIdentifiers'][0]['identifier']) 43 44# ループ 45for item in json_dict['items']: 46 iIs = item['volumeInfo']['industryIdentifiers'] 47 for iI in iIs: 48 print( iI['type'], iI['identifier'])
ISBN_10 0773505997 ISBN_10 0773505997 ISBN_13 0773509999 ISBN_20 1111 ISBN_23 2222

投稿2017/08/24 06:40

can110

総合スコア38266

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

こうですかね。

python

1(element for element in json_dict['industryIdentifiers'] 2 if element['indentifier'] == '0773505997').next()

投稿2017/08/24 05:44

yuba

総合スコア5568

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sizumita

2017/08/24 05:52 編集

いや、ISBNの値が取れるようにしたいのです。 いろいろ本に関するデータがあって、それのISBNから検索がしたいんです。 それと、このコードを実行すると、 KeyError: 'industryIdentifiers' になってしまいます。
yuba

2017/08/24 05:51

では、こんな? (element['indentifier'] for element in json_dict['industryIdentifiers'] if element['type'] == 'ISBN_10').next()
sizumita

2017/08/24 05:54 編集

これも KeyError: 'industryIdentifiers' になってしまいます。 itemsの中にあるので、なぜか取れないんです。
yuba

2017/08/24 05:57

質問文の通りのオブジェクトを渡しているならそのKeyErrorは起こりえないはずですが。 どんなオブジェクトを渡そうとしていますか? もしかしてitemsオブジェクトを渡そうとしていますか?
sizumita

2017/08/24 06:00

itemsの中の、industryIdentifiersの中の、indentifierの中の値が取りたいと言うことです。
yuba

2017/08/24 06:06

itemsはリストですから、例では本が一件しか入っていませんが当然複数入っている可能性があります。 その際にどういう値が得たいのかは決めてありますか?
sizumita

2017/08/24 06:10 編集

どれでもいいので、1つだけISBNの値が欲しいです。
yuba

2017/08/24 06:10

ゼロ件だった場合は?
sizumita

2017/08/24 06:13

なかった場合はifで別の処理をさせることにしています。
yuba

2017/08/24 06:19

では、こうです。 ゼロ件だった場合には例外StopIterationが飛ぶのでexceptして例外処理へ進んでください。 (code['indentifier'] for item in items for code in item['industryIdentifiers'] if code['type'] == 'ISBN_10').next() 次回質問するときは、このコメント欄の問答で説明してもらった仕様まで質問文内で記載できるといいですね。
sizumita

2017/08/24 06:44 編集

すみません、こっちが間違っていたみたいで、'volumeInfo'の中の'industryIdentifiers'でした。
guest

0

(コメント欄に書くつもりのテキストを誤投稿)

投稿2017/08/24 06:19

編集2017/08/24 06:20
yuba

総合スコア5568

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問