Use Python to manipulate the 2nd part of the YAML library

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.