最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python Yaml、Json、Dict之間的轉化

 更新時間:2020年10月19日 11:13:43   作者:Blue·Sky  
這篇文章主要介紹了python Yaml 、Json 、Dict 之間的轉化的示例,幫助大家更好的理解和學習python,感興趣的朋友可以了解下

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)禁止執(zhí)行任意函數(shù)

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 執(zhí)行后如下含有警告
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)禁止執(zhí)行任意函數(shù)

3.解決方法

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

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

Loader的幾種加載方式

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

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

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

相關文章

  • pyautogui自動化控制鼠標和鍵盤操作的步驟

    pyautogui自動化控制鼠標和鍵盤操作的步驟

    這篇文章主要介紹了pyautogui自動化控制鼠標和鍵盤操作的步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Python異常處理總結

    Python異常處理總結

    這篇文章主要介紹了Python異常處理總結,需要的朋友可以參考下
    2014-08-08
  • 使用numpy實現(xiàn)topk函數(shù)操作(并排序)

    使用numpy實現(xiàn)topk函數(shù)操作(并排序)

    這篇文章主要介紹了使用numpy實現(xiàn)topk函數(shù)操作(并排序),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 關于Python中的同步異步阻塞與非阻塞

    關于Python中的同步異步阻塞與非阻塞

    這篇文章主要介紹了關于Python中的同步異步阻塞與非阻塞,具有一定的參考價值,有需要的朋友可以看一下
    2023-03-03
  • caffe的python接口生成配置文件學習

    caffe的python接口生成配置文件學習

    這篇文章主要介紹了caffe的python接口生成配置文件學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Python練習之ORM框架

    Python練習之ORM框架

    這篇文章主要介紹了Python練習之ORM框架,通過使用SQLObject框架操作MySQL數(shù)據(jù)庫展開文章主題詳細內容,具有一定的參考價值,需要的朋友可以參考一下
    2022-06-06
  • Python 獲取ftp服務器文件時間的方法

    Python 獲取ftp服務器文件時間的方法

    今天小編就為大家分享一篇Python 獲取ftp服務器文件時間的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python中UiAutomation庫的使用

    Python中UiAutomation庫的使用

    UiAutomation庫主要用于自動化測試和 UI 操作的場景,本文就來介紹一下Python中UiAutomation庫的使用,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-12-12
  • python實現(xiàn)簡單http服務器功能

    python實現(xiàn)簡單http服務器功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)簡單http服務器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • 基于Python開發(fā)云主機類型管理腳本分享

    基于Python開發(fā)云主機類型管理腳本分享

    這篇文章主要為大家詳細介紹了如何基于Python開發(fā)一個云主機類型管理腳本,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-02-02

最新評論

城口县| 桂东县| 彩票| 美姑县| 合川市| 苏尼特右旗| 龙州县| 湖南省| 舞阳县| 共和县| 泗洪县| 内丘县| 石景山区| 古交市| 黄陵县| 遵义县| 南宫市| 布尔津县| 土默特左旗| 桃园县| 滦南县| 阿克陶县| 白朗县| 工布江达县| 永济市| 英山县| 甘肃省| 丹凤县| 通州区| 同心县| 祁东县| 拜泉县| 杂多县| 香港| 长兴县| 青冈县| 贵南县| 买车| 保靖县| 磴口县| 仪征市|