您好,登錄后才能下訂單哦!
本篇文章為大家展示了python Yaml、Json、Dict之間如何進行轉化,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
Json To Dict
import json jsonData = '{"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 json textDict = {"a":1,"b":2,"c":3,"d":4,"e":5} print(textDict) print(type(textDict)) # 字典轉化為json textJson = 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 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(type(dictText)) yamlText = yaml.dump(dictText) print(yamlText) print(type(yamlText)) #############################3 <class 'dict'> 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 'str'>
Json To Yaml
Json -> Dict -> Yaml
import json,yaml jsonData = '{"listDict":{"a":1,"b":2,"c":3,"d":4,"e":5}}'; print(jsonData) print(type(jsonData)) # Json -> Dict text = json.loads(jsonData) print(text) print(type(text)) # Dict -> Yaml textYaml = 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 yaml 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''' print(yamlText) print(type(yamlText)) # Yaml -> Dict dictText = yaml.load(yamlText,Loader=yaml.FullLoader) print(dictText) print(type(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 <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 yaml 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''' print(yamlText) print(type(yamlText)) # yaml -> dict 沒有設置 ,Loader=yaml.FullLoader 執行后如下含有警告 dictText = yaml.load(yamlText) print(dictText) print(type(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 <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 default
Loader 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的幾種加載方式
上述內容就是python Yaml、Json、Dict之間如何進行轉化,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。