国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

python Yaml、Json、Dict之間的轉化

瀏覽:2日期:2022-07-07 18:16:06

Json To Dict

import jsonjsonData = ’{'a':1,'b':2,'c':3,'d':4,'e':5}’;print(jsonData)print(type(jsonData))text = json.loads(jsonData)print(text)print(type(text))#######################{'a':1,'b':2,'c':3,'d':4,'e':5}<class ’str’>{’a’: 1, ’b’: 2, ’c’: 3, ’d’: 4, ’e’: 5}<class ’dict’>

Dict To Json

import jsontextDict = {'a':1,'b':2,'c':3,'d':4,'e':5}print(textDict)print(type(textDict))# 字典轉化為jsontextJson = json.dumps(textDict)print(textJson)print(type(textJson))########################{’a’: 1, ’b’: 2, ’c’: 3, ’d’: 4, ’e’: 5}<class ’dict’>{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}<class ’str’>

Dict To Yaml

import yamldictText = { '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(type(dictText))yamlText = yaml.dump(dictText)print(yamlText)print(type(yamlText))#############################3<class ’dict’>apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: admin-userroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-system<class ’str’>

Json To Yaml

Json -> Dict -> Yaml

import json,yamljsonData = ’{'listDict':{'a':1,'b':2,'c':3,'d':4,'e':5}}’;print(jsonData)print(type(jsonData))# Json -> Dicttext = json.loads(jsonData)print(text)print(type(text))# Dict -> YamltextYaml = yaml.dump(text)print(textYaml)print(type(textYaml))#############################{'listDict':{'a':1,'b':2,'c':3,'d':4,'e':5}}<class ’str’>{’listDict’: {’a’: 1, ’b’: 2, ’c’: 3, ’d’: 4, ’e’: 5}}<class ’dict’>listDict: a: 1 b: 2 c: 3 d: 4 e: 5<class ’str’>

Yaml -> Dict

import yamlyamlText =’’’apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: admin-userroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-system’’’print(yamlText)print(type(yamlText))# Yaml -> DictdictText = yaml.load(yamlText,Loader=yaml.FullLoader)print(dictText)print(type(dictText))#############################apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: admin-userroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-system<class ’str’>{’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’}]}<class ’dict’>

關于 yaml -> dict 需要注意

yaml 5.1版本后棄用了yaml.load(file)這個用法,因為覺得很不安全,5.1版本之后就修改了需要指定Loader,通過默認加載​​器(FullLoader)禁止執行任意函數

import yamlyamlText =’’’apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: admin-userroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-system’’’print(yamlText)print(type(yamlText))# yaml -> dict 沒有設置 ,Loader=yaml.FullLoader 執行后如下含有警告dictText = yaml.load(yamlText)print(dictText)print(type(dictText))#########################apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: admin-userroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-system<class ’str’>/Users/yyj/Desktop/Project/HelloBike/TimeCalc/pydict2json/dict2json.py:88: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details. dictText = yaml.load(yamlText){’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’}]}<class ’dict’>

1、警告提示

YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the defaultLoader is unsafe. Please read https://msg.pyyaml.org/load for full details.

2.主要原因

yaml 5.1版本后棄用了yaml.load(file)這個用法,因為覺得很不安全,5.1版本之后就修改了需要指定Loader,通過默認加載​​器(FullLoader)禁止執行任意函數

3.解決方法

1.yaml.load(f, Loader=yaml.FullLoader)

2.yaml.warnings({’YAMLLoadWarning’: False}) # 全局設置警告,不推薦

Loader的幾種加載方式

BaseLoader--僅加載最基本的YAML SafeLoader--安全地加載YAML語言的子集。建議用于加載不受信任的輸入。 FullLoader--加載完整的YAML語言。避免任意代碼執行。這是當前(PyYAML 5.1)默認加載器調用yaml.load(input)(發出警告后)。 UnsafeLoader--(也稱為Loader向后兼容性)原始的Loader代碼,可以通過不受信任的數據輸入輕松利用。

至此,Yaml 、Json 、Dict 之間的轉化 介紹完了

以上就是python Yaml 、Json 、Dict 之間的轉化的詳細內容,更多關于python Yaml 、Json 、Dict的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 日韩欧美在线观看视频 | 欧亚毛片| 久久久久琪琪精品色 | 欧美精品成人一区二区视频一 | 亚洲精品久久久久综合中文字幕 | 怡红院精品视频 | 仑乱高清在线一级播放 | 久草免费色站 | 日韩欧美一区二区在线观看 | 长腿校花被啪到腿软视频 | 久草最新视频 | 午夜国产精品不卡在线观看 | 亚洲国产系列久久精品99人人 | 亚洲精品国产一区二区图片欧美 | 久久免费在线观看 | 亚洲精品国产免费 | 国产小片 | 成年人网站免费在线观看 | 欧美国产精品不卡在线观看 | 男女性高爱潮免费的国产 | 国产视频精品久久 | 国产成人精品福利网站在线 | 日日干日日操日日射 | 一级国产a级a毛片无卡 | 日韩精品一级毛片 | a级国产精品片在线观看 | 欧美亚洲一区二区三区在线 | 国产成人免费影片在线观看 | 国产欧美精品区一区二区三区 | 欧美一线不卡在线播放 | 欧美色综合高清视频在线 | 日韩三级黄色片 | 亚洲国产精品一区二区九九 | 欧美视频在线观看网站 | 日本无卡码一区二区三区 | 97高清国语自产拍中国大陆 | 久久亚洲精品中文字幕亚瑟 | 亚洲欧美视频一区二区 | 日本视频在线免费观看 | 国产亚洲在线 | 男女男精品视频免费观看 |