介绍
本篇文章为大家展示了python Yaml、Json、Dict之间如何进行转化,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
<强> Json对Dict 强>
进口json jsonData=https://www.yisu.com/zixun/' {“a”: 1、“b”: 2,“c”: 3,“d”: 4,“e”: 5} '; 打印(jsonData) print(类型(jsonData)) 文本=json.loads (jsonData) 打印(文本) print(类型(文本)) ####################### {“a”: 1、“b”: 2,“c”: 3,“d”: 4,“e”: 5} <类的str> {“a”: 1、“b”: 2,“c”: 3, ' d ': 4,“e”: 5} <类的dict>
<强> dict Json 强>
进口json textDict={“a": 1、“b": 2,“c": 3,“d": 4,“e": 5} 打印(textDict) print(类型(textDict)) #字典转化为json textJson=json.dumps (textDict) 打印(textJson) print(类型(textJson)) # # # # # # # # # # # # # # # # # # # # # # # # {& # 39;一个# 39;:1 & # 39;b # 39;: 2 & # 39; c # 39;: 3 & # 39; d # 39;: 4日& # 39;e # 39;: 5} & lt;类& # 39;dict # 39;比; {“a": 1、“b": 2,“c": 3,“d": 4,“e": 5} & lt;类& # 39;str # 39;在
<强> Dict Yaml 强>
进口yaml dictText={ “apiVersion":“rbac.authorization.k8s.io/v1" “kind":“ClusterRoleBinding" “metadata": { “name":“admin-user" }, “roleRef": { “apiGroup":“rbac.authorization.k8s.io" “kind":“ClusterRole" “name":“cluster-admin" }, “subjects":( { “kind":“ServiceAccount" “name":“admin-user" “namespace":“kube-system" } ] } print(类型(dictText)) yamlText=yaml.dump (dictText) 打印(yamlText) print(类型(yamlText)) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 3 & lt;类& # 39;dict # 39;比; apiVersion: rbac.authorization.k8s.io/v1 :ClusterRoleBinding 元数据: 名称:admin用户切换 roleRef: apiGroup: rbac.authorization.k8s.io :ClusterRole 名称:集群管理员 主题: 类型:ServiceAccount 名称:admin用户切换 名称空间:kube-system & lt;类& # 39;str # 39;在
<强> Json Yaml 强>
<强> Json→Dict→Yaml 强>
进口json, yaml jsonData=https://www.yisu.com/zixun/' {“listDict”: {“a”: 1、“b”: 2,“c”: 3,“d”: 4,“e”: 5}} '; 打印(jsonData) print(类型(jsonData)) # Json -> Dict类型 文本=json.loads (jsonData) 打印(文本) print(类型(文本)) # Dict类型-> Yaml textYaml=yaml.dump(文本) 打印(textYaml) print(类型(textYaml)) ############################# {" listDict ": {“a”: 1、“b”: 2,“c”: 3,“d”: 4,“e”: 5}} <类的str> {“listDict”: {“a”: 1、“b”: 2,“c”: 3, ' d ': 4,“e”: 5}} <类的dict> listDict: 答:1 b: 2 c: 3 d: 4 艾凡:5 <类的str>
<强> Yaml→Dict 强>
进口yaml yamlText=& # 39; & # 39; & # 39; apiVersion: rbac.authorization.k8s.io/v1 :ClusterRoleBinding 元数据: 名称:admin用户切换 roleRef: apiGroup: rbac.authorization.k8s.io :ClusterRole 名称:集群管理员 主题: 类型:ServiceAccount 名称:admin用户切换 名称空间:kube-system& # 39; & # 39; & # 39; 打印(yamlText) print(类型(yamlText)) # Yaml→Dict dictText=yaml.load (yamlText装载机=yaml.FullLoader) 打印(dictText) print(类型(dictText)) ############################# apiVersion: rbac.authorization.k8s.io/v1 :ClusterRoleBinding 元数据: 名称:admin用户切换 roleRef: apiGroup: rbac.authorization.k8s.io :ClusterRole 名称:集群管理员 主题: 类型:ServiceAccount 名称:admin用户切换 名称空间:kube-system & lt;类& # 39;str # 39;比; {& # 39;apiVersion& # 39;: & # 39; rbac.authorization.k8s.io/v1 # 39;, & # 39;这种# 39;:& # 39;ClusterRoleBinding& # 39;, & # 39;元数据# 39;:{& # 39;名字# 39;:& # 39;admin用户切换# 39;},& # 39;roleref # 39;: {& # 39; apiGroup& # 39;: & # 39; rbac.authorization.k8s.io& # 39;, & # 39;这种# 39;:& # 39;ClusterRole& # 39;, & # 39;名字# 39;:& # 39;cluster-admin& # 39;}, & # 39;主题# 39;:[{& # 39;这种# 39;:& # 39;ServiceAccount& # 39;, & # 39;名字# 39;:& # 39;admin用户切换# 39;,& # 39;名称空间# 39;:& # 39;kube-system& # 39;}]} & lt;类& # 39;dict # 39;在python Yaml、Json、Dict之间如何进行转化