2017-05-23 1 views
1

このような文字列をどのように解析してデータ構造にするのですか?私はどのような解析ツールを使うべきですか?onenoteレスポンス文字列からのpythonテキスト解析

u'{\r\n "@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(parentNotebook(id,name,self),parentSectionGroup(id,name,self))","value":[\r\n {\r\n  "id":"0-AB87696357344A7E!2881","self":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881","createdTime":"2017-05-18T01:14:32.977Z","name":"Untitled Section","createdBy":"Jason","createdByIdentity":{\r\n  "user":{\r\n   "id":"AB87696357344A7E","displayName":"Jason"\r\n  }\r\n  },"lastModifiedBy":"Jason","lastModifiedByIdentity":{\r\n  "user":{\r\n   "id":"AB87696357344A7E","displayName":"Jason"\r\n  }\r\n  },"lastModifiedTime":"2017-05-18T02:19:00.587Z","isDefault":false,"pagesUrl":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881/pages","[email protected]":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentNotebook(id,name,self)/$entity","parentNotebook":{\r\n  "id":"0-AB87696357344A7E!2879","name":"Companies and sectors","self":"https://www.onenote.com/api/v1.0/me/notes/notebooks/0-AB87696357344A7E!2879"\r\n  },"[email protected]":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentSectionGroup(id,name,self)/$entity","parentSectionGroup":null\r\n }\r\n ]\r\n}' 
+0

を*あなたは*のように読める何を考えていますか? –

+0

'json'モジュールをインポートします。そして、 'obj = json.loads(your_string)'を使います。 'print(obj [" @ odata.context "])'と言うことができます。 – SuperSaiyan

+0

@Stephen Rauch読書は何ですか?もしあなたがそれを読もうとすると、あなたはそれを解析するでしょうか? – jason

答えて

2

コメントに記載されているSuperSaiyanのとおり、これはJSON文字列です。そう簡単にJSONライブラリで解析されます。

import json 
json_data = json.loads(
    u'{\r\n "@odata.context":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(parentNotebook(id,name,self),parentSectionGroup(id,name,self))","value":[\r\n {\r\n  "id":"0-AB87696357344A7E!2881","self":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881","createdTime":"2017-05-18T01:14:32.977Z","name":"Untitled Section","createdBy":"Jason","createdByIdentity":{\r\n  "user":{\r\n   "id":"AB87696357344A7E","displayName":"Jason"\r\n  }\r\n  },"lastModifiedBy":"Jason","lastModifiedByIdentity":{\r\n  "user":{\r\n   "id":"AB87696357344A7E","displayName":"Jason"\r\n  }\r\n  },"lastModifiedTime":"2017-05-18T02:19:00.587Z","isDefault":false,"pagesUrl":"https://www.onenote.com/api/v1.0/me/notes/sections/0-AB87696357344A7E!2881/pages","[email protected]":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentNotebook(id,name,self)/$entity","parentNotebook":{\r\n  "id":"0-AB87696357344A7E!2879","name":"Companies and sectors","self":"https://www.onenote.com/api/v1.0/me/notes/notebooks/0-AB87696357344A7E!2879"\r\n  },"[email protected]":"https://www.onenote.com/api/v1.0/$metadata#me/notes/notebooks(\'0-AB87696357344A7E%212879\')/sections(\'0-AB87696357344A7E%212881\')/parentSectionGroup(id,name,self)/$entity","parentSectionGroup":null\r\n }\r\n ]\r\n}' 
) 
print(type(json_data)) 
print(json_data['value'][0]['createdBy']) 

結果:

<type 'dict'> 
Jason 
+0

ありがとうございます。私はそれを後でデータフレームに入れます。 'obj = json.loads(r.text) df = pd.DataFrame(obj.get( 'value'、[]))'。 'df'で非常に読みやすい – jason