zoukankan      html  css  js  c++  java
  • 12.yaml的简单使用

    YAML讲义

    字典

    {"age" : "18", "name" : "liqing"}
    
    age: "18"
    

    列表

    [1, 2, xiaoming]
    
    - "1"
    - "2"
    - "xiaoming"
    

    字典和列表的嵌套

    • 字典与字典的嵌套
    {person1:{age:18, name : xiaoming}, person2:{age:19, name : xiaohong} , num: 20}
    
    person1:
      age: "18"
      name: "xiaoming"
    person2:
      age: "19"
      name: "xiaohong"
    num: "20"
    
    • 字典嵌套列表
    {person1: xiaoming, person2: [1, 2, 3]}
    
    person1: "xiaoming"
    person2: 
      - "1"
      - "2"
      - "3"
    
    • 列表嵌套字典
    [{person1:18}, 2, 3, {person2:20}]
    
    - 
      person1: "18"
    - "2"
    - "3"
    - 
      person2: "20"
    
    • 列表嵌套列表
    [1, 2, [3, 4, 5], 6]
    
    - "1"
    - "2"
    - 
      - "3"
      - "4"
      - "5"
    - "6"
    
    • yaml 的读
    import yaml
    with open("./data.yaml") as f:
      yaml.load(f)
    
    • yaml 的写
    import yaml
    aproject = {'name': 'Silenthand Olleander',
                'race': 'Human',
                'traits': ['ONE_HAND', 'ONE_EYE']
                }
    
    print(yaml.dump(aproject,))
    
    • 练习1
    {key1 : {age: 18, name: xiaoming, person: [1, 2, 3, {person2: 4, person3: 5}], height: 100}, key2:[7, 8, 9]}
    
    key1: 
      age: "18"
      name: "xiaoming"
      person:
        - "1"
        - "2"
        - "3"
        - 
           person2: "4"
           person3: "5"
      height: "100"
    key2:
      - "7"
      - "8"
      - "9"
    
    • 练习2
    [1, 2, 3, [4,5,6], {person:[1, 2, 3,]}, 5, {age:18,name:xiaoming}, [7,{age:18,name: xiaohong}] ]
    
    - "1"
    - "2"
    - "3"
    - 
      - "4"
      - "5"
      - "6"
    - 
      person: 
        - "1"
        - "2"
        - "3"
    - "5"
    - 
      age: "18"
      name: "xiaoming"
    - 
      - "7"
      - 
         age: "18"
         name: "xiaohong"
    
    • 纯量
    str1: "1"
    str2: "x"
    int1: 1
    int2: 20
    decimal1: 3.14
    float1: 3.1415926535897932384626433
    boolean1: True
    boolean2: False
    boolean3: true
    boolean4: TRUE
    null1: null
    null2: Null
    null3: NULL
    time1: 2018-01-01 10:38:20.2060
    
    • 布尔类型仅支持全大写,全小写,首字母大写
    • 空值使用null来表示
    • yaml 中不是字符串的包括:整数, float, bool, none, time
    • 锚点和引用
    data: &info
      value: "456"
    name:
      value1: "123"
      <<: *info
    
    • 常见错误
      • yaml 中包含中文
      • yaml 中空格位数不对
      • yaml 中的数据是直接复制的,使用了 tab 键,pycharm 不能转换
      • yaml 不能表示元组(yaml 是各种语言通用的)
  • 相关阅读:
    XtraBackup2.3.3安装配置使用(innobakupex)
    MySQL主从配置问题整理
    saltstack之(十二)配置管理mount
    常用HTTP状态码和CURL 000问题
    RHEL6解决无法使用YUM源问题
    zabbix监控MySQL
    ELK-Python(三)
    解决eclipse项目下出现deployment descriptor和jax-ws web services
    【未来畅想】未来的电信通讯行业,账号密码将取代sim卡
    最新samba.tar.gz安装方法
  • 原文地址:https://www.cnblogs.com/liqing45/p/9504145.html
Copyright © 2011-2022 走看看