python parametric read the contents of the file yaml

A, YAML installation:

 

 

Two, YAML of basic grammar:

. A case sensitive;

b. Align indentation (note indentation similar consistency, and a first indent only empty space)

A mix of the following interview, I hope to read out the reason (started experiencing pit, the weak foundation of key-value form, starting with the first indent is two spaces, leading to the result that the extra string, not French dictionary into show):

 

Third, file reads and parses YMAL

Import YAML
 Import os 

DEF readyml (yamlPath):
     IF  not os.path.isfile (yamlPath):
         The raise FileNotFoundError ( " file path does not exist, please check the Road King is correct:% S " % yamlPath)
     # Open method to open directly read out 
    Open = F (yamlPath, ' R & lt ' , encoding = ' UTF-. 8 ' ) 
    CFG = reached, f.read ()
     Print (type (CFG))
     # converted to a dictionary 
    D = yaml.load (CFG)
     Print (type (D))
     Print ( "Test data read file:% S " % D)
     return D 

IF  the __name__ == ' __main__ ' : 
    yamlPath = " ../testFile/login_testData.yaml " 
    D = readyml (yamlPath)
     Print (D [0] [ " username " ])

The results show the following:

Test file data read: 
[{ ' username ' : ' Test01 ' , ' password ' : ' 1234 ' }, { ' username ' : ' Test02 ' , ' password ' : ' 1232 ' }, { ' username ' : ' TEST03 ' , ' password ' : ' 1233 ' }]

Test01

Guess you like

Origin www.cnblogs.com/irisWhq/p/12084390.html