Note || Python3 of advanced read and write configuration files yaml

yaml is designed to write the language profile, simple and powerful, convenient format than JSON, yaml have PyYAML installation package python language.

       - you first need to install pip: pip install PyYAML

       - yaml basic rules of grammar: case sensitive

                                          Use indention hierarchy

                                          When the Tab key to indent are not allowed, only allows the use of space

                                          # Denotes a comment

------------------------------------ Python code reads yaml file -------- ------------------------------

      Import yaml

      import os

      # Get the current path of the folder where the script

      curpath = os.path.dirname(os.path.realpath(__file__))

      # print(curpath)

      # Get file path yaml

      yamlpath = os.path.join(curpath, "cfgyaml.yaml")

      # Open method opens the file directly read out

      f = open(yamlpath, 'r', encoding='utf-8')

      cfg = f.read()

      print(type(cfg))

      # print(cfg)

      # Dictionary with load transfer method

      d = yaml.load (cfg)

      print(type(d))

      print(d)

--------------------------------- cfgyaml.yaml document reads as follows ----------- --------------------------- 

# Yaml in the array, the equivalent of the list in python

- admin1: 123456

- admin2: 234567

- admin3: 345678

# Yaml key-value pairs, equivalent to the dictionary in python

# test1: 

#     user: username

Pw #: 123456

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

Dictionary written by nesting module dictionary yaml this complex data, there will be braces {}, is not a true yaml file data can be resolved by ruamel module.

Installation: PIP install ruamel.yaml

Use with yaml almost, just use the dump method one more parameter: Dumper = yaml.RoundTripDumper

--------------------------- Python code into the file yaml ----------------- -----------------

import os

from ruamel import yaml

# The dictionary is written to yaml

desired_caps = {

                       'platformName': 'Android',

                       'platformVersion': '7.0',

                       'deviceName': 'ASRNW1111111111'

                       'appPackage': 'com.tencent.com',

                       'appActivity': 'ui.LauncherUI',

                       'automationName': 'Uiautomator2',

                       'unicodeKeyboard': True,

                       'resetKeyboard': True,

                       Moresheth ': True,

                       'chromeOptions': {'androidProcess': 'com.tencent.com'}

}

curpath = os.path.dirname(os.path.realpath(__file__))

yamlpath = os.path.join(curpath, "cfgyaml.yaml")

 

# Write to file yaml

with open(yamlpath, 'w', encoding="utf-8") as f:

      yaml.dump(desired_caps, f, Dumper=yaml.RoundTripDumper)

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

Use ruamel.yaml yaml module can read files, use the previous method is relatively yaml.load added an extra parameter: Loader = yaml.Loader

Such as:

     rea = open(yamlpath, 'r')

     a = rea.load()

     yaml.load b = (a, Loader = yaml.Loader)

     print(b)

Guess you like

Origin www.cnblogs.com/peipei-Study/p/12100724.html