Python Read Yaml file

Source: Internet
Author: User

Recently saw a lot of use of Yaml file as a configuration file or data file project, then also studied, found that Yaml has several advantages: good readability, and scripting language interactivity (really very good), the use of the implementation of the language data type, have a consistent data model, easy to implement.

Since there are so many benefits, why not, and then start studying how to read the Yaml file in Python, let's look at the following:

1, the first need to download the Python Yaml library pyyaml,:http://pyyaml.org/, the installation process is omitted ...

2. Set up a. py file

3. Import Yaml

4, F = open ("Test.yaml")

5. Print yaml.load (f)

You will find that the print is a dictionary, by the way, Python reads the Yaml file, it is stored as a dictionary, we look at the specific code:

Yaml file

Name:tom smithage:37spouse:    name:jane Smith    Age:25children:-name:jimmy Smith   Age:15-name1:jenny Smi Th   Age1:12

Python code:

Import yaml,os# Get file full path filename = Os.path.join (Os.path.dirname (__file__), ' Test.yaml '). replace ("\ \", "/") F = open ( FileName) y = yaml.load (f) Print S

Operation Result:

{' Age ': Panax notoginseng, ' spouse ': {' age ': ' Name ': ' Jane Smith '}, ' name ': ' Tom Smith ', ' children ': [{' Age ': ' ' ' ' ' name ': ' Jimmy Smit H '}, {' Age1 ': ' name1 ': ' Jenny Smith '}]}

is not very convenient, haha.

Python Read Yaml file

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.