The application of Yaml in Python __python

Source: Internet
Author: User

Yaml is an intuitive data serialization format that can be read by a computer, easily readable by humans, and easy to interact with scripting languages. Yaml is similar to XML, but syntax is much simpler than XML and is very simple and efficient for converting data in an array or hash.

YAML Syntax Rules:

http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/

http://www.yaml.org/

Yaml is considered by many to be a file format that transcends XML and JSON. Compared to XML, it is simple enough and easy to use in addition to the many advantages of having XML. And for Json,yaml to be written as a normalized configuration file (which I think is a lot better than JSON, it's crazy to write profiles with JSON).

YAML uses host language data types, which can cause compatibility problems when circulated in multiple languages.

How to write Yaml.      (copy) Yaml code Name:tom Smith age:37 Spouse:name:Jane Smith age:25 Children:-Name:jimmy Smith Age:15-name1:jenny Smith Age1:12

For specific syntax, refer to YAML syntax rules.

--------------------------------------------------------------------------------------------

Yaml's specific implementation in Python: Pyyaml

Write Yaml as a configuration script Test.yaml, the following describes how to read and write Yaml configuration.

Use Python's Yaml library Pyyaml. http://pyyaml.org/

You can use it after you install it under Python lib.

Python Code #加载yaml import yaml #读取文件 f = open (' Test.yaml ') #导入 x = Yaml.load (f) Print X

Perhaps you will get the following similar Strings:python code {' Age ': Panax notoginseng, ' spouse ': {' age ': ', ' name ': ' Jane Smith '}, ' name ': ' Tom Smith ', ' Children ': [ {' Age ':, ' name ': ' Jimmy Smith '}, {' Age1 ': ' name1 ': ' Jenny Smith '}]}

Using the Yaml library in Python is simple, and basically uses two functions:

Yaml.load

Yaml.dump

For the use of pickle shoes, this means that there is no need to elaborate.

Warning:it is isn't safe to call Yaml.load with any data received from a untrusted source! yaml.load is as powerful as Pickle.load and so may call the any Python function.

The hardest part of reading for YAML is to write the correct YAML data format. If accidentally error, will cause the load exception, but sometimes there is no abnormal report, but will not read any data.

Pyyaml is a complete Python implementation, claiming to be more NB than pickle. (Who knows.) )

Yaml.load accepts a byte string, a Unicode string, an open binary file object, or an open text file object. A byte string or a file must is encoded with utf-8, utf-16-be or utf-16-le encoding. Yaml.load detects the encoding by checking the BOM (byte mark) sequence at the beginning of the St Ring/file. If no BOM is present, theutf-8 encoding is assumed.

Yaml.load can receive a byte string, a Unicode string, an open binary file, or a text file object. byte strings and files must be utf-8,utf-16-be or utf-16-le encoded. Yaml.load confirms the encoding by checking the BOM (byte order mark) at the start of the string/file. If there is no BOM, the default is Utf-8.

Baidu on the BOM in the UCS code is called "ZERO WIDTH no-break Space" character, its code is Feff. Fffe is not present in UCS, so it should not appear in the actual transmission. UCS specification recommended that we transfer the byte stream before the transmission of the character "ZERO WIDTH no-break space." This means that if the recipient receives the Feff, the byte stream is Big-endian, and if Fffe is received, it indicates that the byte stream is Little-endian. Therefore, the character "ZERO WIDTH No-break Space" is also called the BOM.
UTF-8 does not require a BOM to indicate byte order, but you can use a BOM to indicate how the encoding is encoded. The character "ZERO WIDTH no-break Space" UTF-8 code is the EF BB BF. So if the receiver receives the byte stream at the beginning of the EF BB BF, it will know that this is UTF-8 code. Windows uses a BOM to mark the encoding of a text file.

Yaml.load will return a Python object. about what it would be ... Look what your data is ...

If a string or a file contains several documents, you could load them all with the yaml.load_all function.

If a string or file contains several pieces of YAML documents, you can use Yaml.load_all to parse all the documents.

Python code yaml.load (stream, Loader=<class ' Yaml.loader.Loader ' >) Parse the ' the ' Yaml document in a stream #只      Resolves the first and produce the corresponding Python object. Yaml.load_all (Stream, Loader=<class ' Yaml.loader.Loader ' >) Parse all YAML documents in a stream and prod UCE corresponding Python objects.

Yaml.load_all will generate an iterator, all you have to do is for read out

  Python code    documents =  ""   name: the set of gauntlets   ' Pauraegen '   description: >    a set of handgear with  sparks that crackle    across its knuckleguards.   ---  name: the set of gauntlets  ' Paurnen '   description: >      A set of gauntlets that gives off a foul,      acrid odour yet remains untarnished. 

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.