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