+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

从Python的文本文件中读取多级,复杂的字典?

发布于2020-11-28 09:05     阅读(77)     评论(0)     点赞(7)     收藏(3)


0

1

2

3

4

5

6

7

8

我得到一个包含多级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"}}}

我正在考虑基于规则的解析,但是那将非常复杂。


解决方案


在这种情况下,解决此问题的最佳方法是将字符串加载到JSON解析器中。这将返回您需要的对象。请使用以下代码(其中'filename'是包含原始字符串的文件的名称)。

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

myJSON现在,对象是字典:

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

0

1

2

3

4

5

6



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/634469/1ef84c43372cded9fb17/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

7 0
收藏该文
已收藏

评论内容:(最多支持255个字符)