2017-03-21 8 views
0

多レベルのPython辞書のプリントアウトを含むテキストファイルが与えられました。それをPythonに「辞書の辞書」として読み込み、階層を保存する簡単な方法は何ですか?Pythonのテキストファイルから多レベルの複雑な辞書を読み込みますか?

{"options":{"version":2,"background":{"enable":"color","enableVR":false,"uid":"51af6a870cce449eb75b0345feebaebb","color":[0.06666666666666667,0.06666666666666667,0.06666666666666667]},"environment":{"uid":"d348dfd2a8104ab0b8528f885d645eb3","enable":false,"exposure":2.1402,"rotation":0,"blur":0.1,"backgroundExposure":1,"shadowEnable":false,"shadowBias":0.005},"lighting":{"enable":true,"lights":[{"type":"DIRECTION","enable":true,"color":[0.7806122448979592,0.7985214493960848,1],"intensity":0.96,"ground":[0.3,0.2,0.2],"intensityGround":1,"matrix":[-0.9815804500275482,0.1909922167095499,-0.004668327333101102,0,0.13548347102422797,0.7131126062169096,0.6878332937084056,0,0.13469984856318096,0.6745312327912614,-0.7258536813891331,0,0.2764793650767136,1.078885802792896,13.000759350324298,1],"falloff":1.187541845331568,"attachedToCamera":true,"angle":45,"hardness":0.5,"castShadows":true,"shadowBias":0.005,"shadowSize":1024},{"type":"DIRECTION","enable":true,"color":[1,0.8783319450229073,0.7091836734693877],"intensity":1,"ground":[0.3,0.2,0.2],"intensityGround":1,"matrix":[0.7755211495104662,-0.0666602860002539,0.6277924441503967,0,0.2690483737361956,0.9344846523281437,-0.233133882464682,0,-0.5711216326246741,0.3497067926613838,0.7426474533145686,0,-0.747616034294845,0.6075892729952792,15.131447202854693,1],"falloff":1.187541845331568,"attachedToCamera":true,"angle":45,"hardness":0.5,"castShadows":true,"shadowBias":0.005,"shadowSize":1024},{"type":"HEMI","enable":true,"color":[0.423469387755102,0.7352665556018328,1],"intensity":0.12,"ground":[0.9642857142857143,0.6484128636877489,0.2607507288629738],"intensityGround":0.06,"matrix":[0.6903299131022926,-0.7076363669144057,-0.15064986988498263,0,-0.011089698239978026,0.19785137301620165,-0.9801692980238416,0,0.7234097246160907,0.6783108479272498,0.12873524736872857,0,1.1306543819885668,1.0843697482030679,14.240705446640208,1],"falloff":1.187541845331568,"attachedToCamera":false,"angle":45,"hardness":0.5,"castShadows":true,"shadowBias":0.005,"shadowSize":1024}]},"scene":{"fov":1,"nearFarRatio":0.005,"postProcess":{"enable":true,"grain":{"enable":false,"animated":false,"factor":0.15},"sharpen":{"enable":false,"factor":0.2},"chromaticAberration":{"enable":false,"factor":0.05},"vignette":{"enable":false,"lens":[0.8,0.25]},"bloom":{"enable":false,"factor":0.5,"threshold":0,"radius":0.7},"toneMapping":{"enable":false,"method":"default","exposure":1,"brightness":0,"contrast":0,"saturation":1},"colorBalance":{"enable":false,"low":[0,0,0],"mid":[0,0,0],"high":[0,0,0]},"dof":{"enable":false,"blurNear":0.5,"blurFar":0.5,"focusPoint":[0,0,0]},"ssao":{"enable":false,"bias":0.009068293352733361,"intensity":0.5,"radius":0.04534146676366681}}},"materials":{"d47e69e8-04c7-4b9a-a025-c86be71f6bb7":{"version":3,"stateSetID":0,"shadeless":false,"id":"d47e69e8-04c7-4b9a-a025-c86be71f6bb7","name":"human.001_0","cullFace":"DISABLE","reflection":0.1,"channels":{"DiffuseColor":{"color":[1,1,1],"factor":1,"enable":true},"DiffuseIntensity":{"color":[1,1,1],"factor":1,"enable":true},"SpecularColor":{"color":[0.5,0.5,0.5],"factor":0,"enable":true},"SpecularHardness":{"color":[1,1,1],"factor":12.5,"enable":true},"Displacement":{"color":[1,1,1],"factor":0,"enable":true},"NormalMap":{"color":[1,1,1],"factor":1,"enable":false,"flipY":true},"BumpMap":{"color":[1,1,1],"factor":1,"enable":false},"Opacity":{"color":[1,1,1],"factor":1,"enable":true,"type":"alphaBlend","invert":false},"EmitColor":{"color":[1,1,1],"factor":0,"enable":true,"type":"additive"},"DiffusePBR":{"color":[1,1,1],"factor":1,"enable":true},"AlbedoPBR":{"color":[1,1,1],"factor":1,"enable":false},"SpecularPBR":{"color":[1,1,1],"factor":0.05,"enable":true},"GlossinessPBR":{"color":[1,1,1],"factor":0.4,"enable":false},"RoughnessPBR":{"color":[1,1,1],"factor":0.6,"enable":true},"MetalnessPBR":{"color":[1,1,1],"factor":0,"enable":false},"SpecularF0":{"color":[1,1,1],"factor":0.5,"enable":false},"AOPBR":{"color":[1,1,1],"factor":1,"enable":false,"occludeSpecular":false},"CavityPBR":{"color":[1,1,1],"factor":1,"enable":false}}},"8d06432a-9927-4748-982c-d1b31d85d2ed":{"version":3,"stateSetID":1,"shadeless":false,"id":"8d06432a-9927-4748-982c-d1b31d85d2ed","name":"Material.001","cullFace":"DISABLE","reflection":0.1,"channels":{"DiffuseColor":{"color":[1,1,1],"factor":1,"enable":true},"DiffuseIntensity":{"color":[1,1,1],"factor":1,"enable":true},"SpecularColor":{"color":[0.5,0.5,0.5],"factor":0,"enable":true},"SpecularHardness":{"color":[1,1,1],"factor":12.5,"enable":true},"Displacement":{"color":[1,1,1],"factor":0,"enable":true},"NormalMap":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"RGB","texCoordUnit":0,"uid":"251c0badfd924b8a8cb08c087e6da38d"},"factor":1,"enable":true,"flipY":true},"BumpMap":{"color":[1,1,1],"factor":1,"enable":false},"Opacity":{"color":[1,1,1],"factor":1,"enable":true,"type":"alphaBlend","invert":false},"EmitColor":{"color":[1,1,1],"factor":0,"enable":true,"type":"additive"},"DiffusePBR":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"RGB","texCoordUnit":0,"uid":"8eeb9ad270f444c192548d8a8e5a6976"},"factor":1,"enable":true},"AlbedoPBR":{"color":[1,1,1],"factor":1,"enable":false},"SpecularPBR":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"RGB","texCoordUnit":0,"uid":"aa3aa7c807424fe09f6de04420200b45"},"factor":1,"enable":true},"GlossinessPBR":{"texture":{"magFilter":"LINEAR","minFilter":"LINEAR_MIPMAP_LINEAR","wrapS":"REPEAT","wrapT":"REPEAT","textureTarget":"TEXTURE_2D","internalFormat":"LUMINANCE","texCoordUnit":0,"uid":"e26b58d50de146909143ca2fb94ce1f4"},"factor":1,"enable":true},"RoughnessPBR":{"color":[1,1,1],"factor":0.20000000298023224,"enable":false},"MetalnessPBR":{"color":[1,1,1],"factor":0,"enable":false},"SpecularF0":{"color":[1,1,1],"factor":0.5,"enable":false},"AOPBR":{"color":[1,1,1],"factor":1,"enable":true,"occludeSpecular":false},"CavityPBR":{"color":[1,1,1],"factor":1,"enable":true}}}},"orientation":{"matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]},"camera":{"position":[-0.11948612417396776,-3.244330914375282,-200.12585703226537],"target":[0.015371265170614823,0.11829936504364014,14.100300312042236]},"animation":{"order":[],"speed":1,"cycleMode":"one","autoplay":false},"hotspot":{"hotspots":[]},"vr":{"scale":0.9068293352733361,"initialCamera":{"position":[0.08103972646236413,-2.0762001369882364,13.491239980506897],"rotation":[0,0,0,1]},"floorHeight":13.491239980506897},"shading":{"pointSize":2,"type":"lit","renderer":"pbr","vertexColor":{"enable":false,"useAlpha":false,"colorSpace":"srgb"}},"wireframe":{"enable":false,"color":"000000FF"}}} 

私はルールベースの解析について考えていますが、それは恐ろしく複雑です。

+4

何をそこに持っていることはありません、JSONのようになります。

import json with open('filename') as file: myString = file.read() myJSON = json.loads(myString) 

myJSONオブジェクトは現在の辞書のですか? ['json'](https://docs.python.org/3/library/json.html)を使ってみましたか? – jonrsharpe

+3

'import json; d = ""; myd = json.loads(d) ' – davedwards

+0

@jonrsharpe true!ありがとう;) – davedwards

答えて

3

この場合、JSONパーサーに文字列をロードすることをおすすめします。これにより、必要なオブジェクトが返されます。次のコードを使用してください('filename'は元の文字列を保持するファイルの名前です)。

>>> myJSON['options']['version'] 
2 
+0

一般にファイルを開くには 'with'コンテキストマネージャを使用し、変数名は' snake_case'でなければなりません。 – jonrsharpe

+0

@jonrsharpe興味深いヘビケース。 Pythonがラクダのケースにヘビのケースを好む理由はありますか?私はバージョンセパレータとして '_'を予約しているので、私は一般的にはラクダのケースを使うのが好きです。例えば、私は通常、 'generate_data'と' generate_data_use_model'の代わりに 'generateData'と' generateData_useModel'を書いています(それはあなたが混乱していると思いますか?) –

+0

@SibbsGamblingこの文脈では "バージョン"公式のPythonスタイルのガイドについては、https://www.python.org/dev/peps/pep-0008/ – jonrsharpe

1

まずは次のように、文字列にファイルの内容をお読みください。

with open("thefile") as f: 
    s = f.read() 

そして、単にeval()組み込み使用:あなたが与えた特定の文字列を

thedict = eval(s) 

、まずない限り、エラーを生成します例えば、

false, true = 0, 1 

falsetrueは、文字列内の変数名のように使用されます。

+2

これは動作しますが、明らかにそれを行う方法の1つ(できれば1つだけです)ではありません。 'False'や' True'ではなく 'false'と' true'を含みます。なぜなら、変数名ではなくJSONであるからです。実際にはPythonプリントアウトだった場合は、デフォルトでシングルクォートを使用しますが、JSONではダブルクォートが必要です。 – jonrsharpe

+1

それは私にもそのように見えますが、OPは "マルチレベルのPython辞書のプリントアウト"だと言いました。彼らに10人の担当者しかいなければ、私は彼らがそれについて混乱していると思う傾向がさらに増すだろう - 言い換えれば、私はそれらを取っている) - –

+1

ありがとう! JSON形式を「多レベルのPython辞書のプリントアウト」と愚かに記述して申し訳ありません! –

関連する問題