zoukankan      html  css  js  c++  java
  • Python yaml处理

    安装方式: pip install pyyaml

    一、module.yaml为

    name: Tom Smith  
    age: 37  
    spouse:  
     name: Jane Smith
     age: 25
    children:  
     - name:
        - wohaoshuai1:
           wohaoshuai2: wohaoshuai2
       age: 15  
     - name1: Jenny Smith  
       age1: 12
       wohaoshuai3: wohaoshuai3
       
    #该yaml文件意思为,第一层字典中有name,age,spouse,children三个值,spouse下还存在一层字典,而children下用这种格式书写后首先会解析出一个列表,在列表
    下会有两个字典,第一个字典的两个键为name和age,而name会再次解析为一个列表,下面有键wohaoshuai1,wohaoshuai1下又存在一个字典,key为wohaoshuai2
    二、test.py为
    1 import yaml
    2 f = open('module.yml','r')
    3 lines = f.read()
    4 #print(lines)
    5 x = yaml.load(lines)
    6 print(x)
    7 f.close()

    三、结果为
    {'name': 'Tom Smith', 'age': 37, 'spouse': {'name': 'Jane Smith', 'age': 25}, 'children': [{'name': [{'wohaoshuai1': {'wohaoshuai2': 'wohaoshuai2'}}], 'age': 15}, {'name1': 'Jenny Smith', 'age1': 12, 'wohaoshuai3': 'wohaoshuai3'}



  • 相关阅读:
    git 修改文件内容
    centos 7 安装gitlab
    安装Git 创建版本库
    安装 jenkins
    LVS 之搭建
    113. Path Sum II
    112. Path Sum
    111. Minimum Depth of Binary Tree
    110. Balanced Binary Tree
    109.Convert sorted list to BST
  • 原文地址:https://www.cnblogs.com/Presley-lpc/p/9533090.html
Copyright © 2011-2022 走看看