Yaml数据读取和数据转化
Yaml文件
familyInfo.yaml
name: Tom Smith age: 37 wife: name: Jane Smith age: 25 children: - name: Jimmy Smith age: 15 - name: Jenny Smith age: 12
yaml数据转化 dump()可以将Python的对象序列转化成YAML流,如果stream是none,那返回的则是字符串
import yaml slogan=['welcome','to','51zxw'] website={'url':'www.12345678'} print(slogan) print(website) print(yaml.dump(slogan)) print(yaml.dump(website))
yaml数据读取 load()解析文件流中的第一个YAML文档并生成相应的Python对象
import yaml file=open('./familyInfo.yaml','r') data=yaml.load(file) print(data) print(data['name']) print(data['age']) print(data['wife']['name']) print(data['wife']['age']) print(data['children']) print(data['children'][0]['name']) print(data['children'][0]['age']) #修改变量的值,但是不会影响yaml文件中的值 data['name']='zhangsan' print(data['name'])