91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python Yaml、Json、Dict之間如何進行轉化

發布時間:2020-10-27 18:41:20 來源:億速云 閱讀:192 作者:Leah 欄目:開發技術

本篇文章為大家展示了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,通過默認加載&#8203;&#8203;器(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,通過默認加載&#8203;&#8203;器(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代碼,可以通過不受信任的數據輸入輕松利用。

上述內容就是python Yaml、Json、Dict之間如何進行轉化,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

大名县| 宁化县| 南靖县| 清镇市| 开鲁县| 双桥区| 牟定县| 巴东县| 枝江市| 公主岭市| 阿尔山市| 射阳县| 尼勒克县| 交城县| 黄龙县| 甘泉县| 临泉县| 陵水| 凤台县| 固阳县| 茂名市| 舒兰市| 仁布县| 鄯善县| 潞城市| 丽水市| 新泰市| 通江县| 金沙县| 平果县| 兴业县| 焦作市| 哈尔滨市| 四平市| 丰镇市| 台北市| 迭部县| 黔江区| 南昌市| 八宿县| 福建省|