python-yaml文件读取
2021/12/22 11:19:33
本文主要是介绍python-yaml文件读取,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# yaml文件读取
# 创建yaml文件
# 语法规则:字典类型的数据 键: 值 冒号后要空格
文件名:test.yaml 文件内容: id: 1 name: '张三' age: 18 sex: '女'
import yaml # 打开文件 f = open('./data/test.yaml','r',encoding='utf-8') # 读取yaml数据 data = yaml.load(f,yaml.FullLoader) print(data) name = data.get('name') name = data['name'] print(name) f.close()
# 打开文件
文件名:test1.yaml 文件内容:
- 1 - '秋水' - 18 - 女
with open('./data/test1.yaml','r',encoding='utf-8') as f: data = yaml.load(f,yaml.FullLoader) name = data[1] print(name)
# 做项目的时候,交互[{}]
文件名:test2.yaml 文件内容:
- id: 1 name: '虚竹' age: 33 sex: '男' - id: 2 name: '小龙女' age: 33 sex: '男'
with open('./data/test2.yaml','r',encoding='utf-8') as f: data = yaml.load(f,yaml.FullLoader) name = data[1].get('name') name1 = data[0]['name'] print(name,name1)
# 做项目的时候,交互[{}]
文件名:test3.yaml 文件内容:
- id: 1 name: '球会' age: 18 sex: '女' data: address: '长沙' status: 'success' - id: 1 name: 'hui会' age: 18 sex: '女' data: address: '长沙1' status: 'success'
with open('./data/test3.yaml', 'r', encoding='utf-8') as f: data = yaml.load(f, yaml.FullLoader) name = data[1]['data']['address'] print(name)
这篇关于python-yaml文件读取的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型