python Yaml、Json、Dict之间的转化


Posted in Python onOctober 19, 2020

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的几种加载方式

  • 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 相关文章推荐
Python ldap实现登录实例代码
Sep 30 Python
Python内建函数之raw_input()与input()代码解析
Oct 26 Python
python中in在list和dict中查找效率的对比分析
May 04 Python
Python中的TCP socket写法示例
May 11 Python
python批量从es取数据的方法(文档数超过10000)
Dec 27 Python
如何在django里上传csv文件并进行入库处理的方法
Jan 02 Python
Python 利用高德地图api实现经纬度与地址的批量转换
Aug 14 Python
Python图像处理库PIL中图像格式转换的实现
Feb 26 Python
python 装饰器功能与用法案例详解
Mar 06 Python
Python标准库json模块和pickle模块使用详解
Mar 10 Python
你应该知道的Python3.6、3.7、3.8新特性小结
May 12 Python
python查看矩阵的行列号以及维数方式
May 22 Python
Python pip 常用命令汇总
Oct 19 #Python
Python环境使用OpenCV检测人脸实现教程
Oct 19 #Python
python Tornado框架的使用示例
Oct 19 #Python
python mock测试的示例
Oct 19 #Python
python 提高开发效率的5个小技巧
Oct 19 #Python
python 利用toapi库自动生成api
Oct 19 #Python
协程Python 中实现多任务耗资源最小的方式
Oct 19 #Python
You might like
解析PHP无限级分类方法及代码
2013/06/21 PHP
php中array_multisort对多维数组排序的方法
2020/06/21 PHP
List the Codec Files on a Computer
2007/06/18 Javascript
JavaScript 利用Cookie记录用户登录信息
2009/12/08 Javascript
node.js中的console.trace方法使用说明
2014/12/09 Javascript
解决jquery实现的radio重新选中的问题
2015/07/03 Javascript
iPhone手机上搭建nodejs服务器步骤方法
2015/07/06 NodeJs
JavaScript中removeChild 方法开发示例代码
2016/08/15 Javascript
vue-router路由简单案例介绍
2017/02/21 Javascript
微信小程序获取手机网络状态的方法【附源码下载】
2017/12/08 Javascript
详解vue的数据劫持以及操作数组的坑
2019/04/18 Javascript
Angular 多级路由实现登录页面跳转(小白教程)
2019/11/19 Javascript
[00:59]DOTA2荣耀之路1:Doom is back!weapon X!
2018/05/22 DOTA
Django中实现一个高性能计数器(Counter)实例
2014/07/09 Python
Python实现的数据结构与算法之链表详解
2015/04/22 Python
Python编程实现数学运算求一元二次方程的实根算法示例
2017/04/02 Python
详谈python read readline readlines的区别
2017/09/22 Python
实用自动化运维Python脚本分享
2018/06/04 Python
python读取excel指定列数据并写入到新的excel方法
2018/07/10 Python
python提取具有某种特定字符串的行数据方法
2018/12/11 Python
基于Python实现视频的人脸融合功能
2020/06/12 Python
Keras loss函数剖析
2020/07/06 Python
StubHub智利:购买和出售您的门票
2016/11/23 全球购物
来自世界各地的饮料:Flavourly
2019/05/06 全球购物
澳大利亚在线划船、露营和钓鱼商店:BCF Australia
2020/03/22 全球购物
2014小学植树节活动总结
2014/03/10 职场文书
幼儿园区域活动总结
2014/05/08 职场文书
我的中国心演讲稿
2014/09/04 职场文书
校长个人总结
2015/03/03 职场文书
2015年生产部工作总结范文
2015/05/25 职场文书
大学学生会竞选稿
2015/11/19 职场文书
护士自荐信范文(2016推荐篇)
2016/01/28 职场文书
《打电话》教学反思
2016/02/22 职场文书
详解Mysql 函数调用优化
2021/04/07 MySQL
Pytorch 如何实现常用正则化
2021/05/27 Python
5种 JavaScript 方式实现数组扁平化
2021/10/05 Javascript