YAML ApplicationThe main point here is to document the application of Yaml in the Python language. Compared to the JSON library, the YAML library has strikingly similar similarities. a Load method, a dump method. It is also a good understanding to know the meaning of the name.
Let's start by creating a Yaml file, as shown in:
The following data are stored:
PNSSpouse: name:jane Smith - name:jimmy Smith- Name1:jenny Smith 12
Load usage
import Yamlstream = open ( " test_yaml.yaml , " r ") # read Yaml file data = Yaml.load (stream) print (Data[ " Children "][0][" name " ] # Span style= "COLOR: #008000" > Gets the value corresponding to the first name in the Yaml file children
Output Result:
C:\Python34\python3.exe e:/project_case/demo1.pyjimmy Smith
The understanding level is understood by the dict in Python.
Dump usage
" "__author__ = ' Fighter007 ' __date__ = ' 2018/2/20 ' __desc__ = YAML application in Python" "Importyamls='I have an apple'Print(Yaml.dump (s)) lists= ['Zhangsan','Lisi','Wangwu','Zhaoliu']Print(Yaml.dump (lists))classPerson (object):def __init__(self, Name, age): Self.name=name Self.age=Agep1= Person ('Zhangsan', 19) P2= Person ('Lisi', 20) P3= Person ('Wangwu'21st) Persons=[P1, p2, p3]Print(Yaml.dump (persons))
Output Result:
I have an apple ... [Zhangsan, Lisi, Wangwu, Zhaoliu] - !! Python/object:__main__. person {age:19, Name:zhangsan}-!! Python/object:__main__. person {age:20, name:lisi}-!! Python/object:__main__. person {age:21, Name:wangwu}
Use Python to manipulate the 2nd part of the YAML library