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

python Yaml、Json、Dict之間的轉(zhuǎn)化

 更新時(shí)間:2020年10月19日 11:13:43   作者:Blue·Sky  
這篇文章主要介紹了python Yaml 、Json 、Dict 之間的轉(zhuǎn)化的示例,幫助大家更好的理解和學(xué)習(xí)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))
# 字典轉(zhuǎn)化為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'>

關(guān)于 yaml -> dict 需要注意

yaml 5.1版本后棄用了yaml.load(file)這個(gè)用法,因?yàn)橛X得很不安全,5.1版本之后就修改了需要指定Loader,通過(guò)默認(rèn)加載​​器(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 沒(méi)有設(shè)置 ,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)這個(gè)用法,因?yàn)橛X得很不安全,5.1版本之后就修改了需要指定Loader,通過(guò)默認(rèn)加載​​器(FullLoader)禁止執(zhí)行任意函數(shù)

3.解決方法

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

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

Loader的幾種加載方式

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

至此,Yaml 、Json 、Dict 之間的轉(zhuǎn)化 介紹完了

以上就是python Yaml 、Json 、Dict 之間的轉(zhuǎn)化的詳細(xì)內(nèi)容,更多關(guān)于python Yaml 、Json 、Dict的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • pyautogui自動(dòng)化控制鼠標(biāo)和鍵盤操作的步驟

    pyautogui自動(dòng)化控制鼠標(biāo)和鍵盤操作的步驟

    這篇文章主要介紹了pyautogui自動(dòng)化控制鼠標(biāo)和鍵盤操作的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python異常處理總結(jié)

    Python異常處理總結(jié)

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

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

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

    關(guān)于Python中的同步異步阻塞與非阻塞

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

    caffe的python接口生成配置文件學(xué)習(xí)

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

    Python練習(xí)之ORM框架

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

    Python 獲取ftp服務(wù)器文件時(shí)間的方法

    今天小編就為大家分享一篇Python 獲取ftp服務(wù)器文件時(shí)間的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • Python中UiAutomation庫(kù)的使用

    Python中UiAutomation庫(kù)的使用

    UiAutomation庫(kù)主要用于自動(dòng)化測(cè)試和 UI 操作的場(chǎng)景,本文就來(lái)介紹一下Python中UiAutomation庫(kù)的使用,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • python實(shí)現(xiàn)簡(jiǎn)單http服務(wù)器功能

    python實(shí)現(xiàn)簡(jiǎn)單http服務(wù)器功能

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

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

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

最新評(píng)論

临泉县| 江安县| 甘肃省| 武隆县| 金门县| 禄丰县| 周至县| 宁安市| 浠水县| 永年县| 余姚市| 合肥市| 奉新县| 政和县| 勃利县| 华坪县| 连云港市| 玉树县| 常山县| 伊金霍洛旗| 天柱县| 武穴市| 昌吉市| 龙陵县| 仪征市| 雅江县| 舟山市| 渑池县| 炉霍县| 鹤庆县| 阿坝| 罗甸县| 凤翔县| 建湖县| 京山县| 个旧市| 沙雅县| 忻州市| 且末县| 兴文县| 东港市|