从Python中的文本文件读入多层次、复杂的词典?

从Python中的文本文件读入多层次、复杂的词典?,python,Python,我得到一个文本文件,其中包含一个多级python字典的打印输出。将其作为“字典中的字典”加载回Python的简单方法是什么,以保留层次结构 {"options":{"version":2,"background":{"enable":"color","enableVR":false,"uid":"51af6a870cce449eb75b0345feebaebb","color":[0.06666666666666667,0.06666666666666667,0.0666666666666666

我得到一个文本文件,其中包含一个多级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"}}}

我正在考虑基于规则的解析,但这会非常复杂

首先将文件内容读入字符串,如:

with open("thefile") as f:
    s = f.read()
然后只需使用内置的
eval()

thedict = eval(s)
对于您给定的特定字符串,除非您首先这样做,否则将产生错误,例如

false, true = 0, 1

因为
false
true
在字符串中用作变量名。

首先将文件内容读入字符串,如:

with open("thefile") as f:
    s = f.read()
然后只需使用内置的
eval()

thedict = eval(s)
对于您给定的特定字符串,除非您首先这样做,否则将产生错误,例如

false, true = 0, 1

因为
false
true
就像字符串中的变量名一样使用。

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

myJSON
对象现在是一个字典:

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

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

myJSON
对象现在是一个字典:

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


你那里的东西看起来像JSON,不是吗?您是否尝试过使用?
导入json;d=“”;myd=json.loads(d)
@jornsharpe true!谢谢;)你那里的东西看起来像JSON,不是吗?您是否尝试过使用?
导入json;d=“”;myd=json.loads(d)
@jornsharpe true!谢谢;)这会起作用,但显然不是一种(最好是唯一的)方法。它包含
false
true
而不是
false
true
,因为它是JSON,而不是因为它们是变量名。如果它实际上是一个Python打印输出,那么默认情况下它也会使用单引号,而JSON需要双引号。在我看来也是这样,但OP说它是“一个多级Python字典的打印输出”。如果他们只有10个代表,我会更倾向于认为他们对此感到困惑——事实上,我相信他们的话——)谢谢!很抱歉愚蠢地将JSON格式描述为“多级python字典的打印输出”!这会起作用,但显然不是一种(最好是唯一的)方法。它包含
false
true
而不是
false
true
,因为它是JSON,而不是因为它们是变量名。如果它实际上是一个Python打印输出,那么默认情况下它也会使用单引号,而JSON需要双引号。在我看来也是这样,但OP说它是“一个多级Python字典的打印输出”。如果他们只有10个代表,我会更倾向于认为他们对此感到困惑——事实上,我相信他们的话——)谢谢!很抱歉愚蠢地将JSON格式描述为“多级python字典的打印输出”!请注意,通常应使用
上下文管理器来打开文件,变量名应为
snake\u case
@jornsharpe。Python为什么喜欢snake-case而不是camel-case?一般来说,我喜欢使用camel case,因为我保留了
\uu
作为版本分隔符。例如,我通常写
generateData
generateData\u useModel
而不是
generateData\u-useModel
generateData\u-useModel
(你认为这里很混乱吗?)@sibbs赌博我不确定你所说的“版本”是什么意思在本文中,要了解官方的Python风格指南,请参阅@jornsharpe,谢谢您的提示!我还没有阅读过PEP8。请注意,您通常应该使用
上下文管理器来打开一个文件,变量名应该是
snake\u case
@jornsharpe。Python为什么喜欢snake-case而不是camel-case?一般来说,我喜欢使用camel case,因为我保留了
\uu
作为版本分隔符。例如,我通常写
generateData
generateData\u useModel
而不是
generateData\u-useModel
generateData\u-useModel
(你认为这里很混乱吗?)@sibbs赌博我不确定你所说的“版本”是什么意思在本文中,要了解官方的Python风格指南,请参阅@jornsharpe,谢谢您的提示!我还没有看完政治公众人物。