相关文章推荐
强健的蚂蚁  ·  Swift ...·  1 年前    · 
听话的硬盘  ·  android.database.sqlit ...·  1 年前    · 
玩足球的企鹅  ·  MuPAD使用总结 - ...·  1 年前    · 
import yaml with open ( 'test_列表.yaml' , encoding = 'utf-8' ) as file1 : data = yaml . load ( file1 , Loader = yaml . FullLoader ) #读取yaml文件 print ( data ) [ 10 , 20 , 30 ] import yaml with open ( 'test_字典.yaml' , encoding = 'utf-8' ) as file1 : data = yaml . load ( file1 , Loader = yaml . FullLoader ) #读取yaml文件 print ( data ) { 'name' : '吴彦祖' , 'age' : 20 }
  • 列表中的字典
  • #test_列表中的字典.yaml
     name: 吴彦祖
     age: 21
     A: apple
    #test.py
    import yaml
    with open('test_列表中的字典.yaml',encoding='utf-8') as file1:
        data = yaml.load(file1,Loader=yaml.FullLoader)#读取yaml文件
        print(data)
    [{'name': '吴彦祖', 'age': 21}, {'A': 'apple'}]
    
  • 字典中的字典
  • #test_字典中的字典.yaml
    name: 
     name1: 吴彦祖
    #test.py
    import yaml
    with open('test_字典中的字典.yaml',encoding='utf-8') as file1:
        data = yaml.load(file1,Loader=yaml.FullLoader)#读取yaml文件
        print(data)
    {'name': {'name1': '吴彦祖'}}
    
  • 字典中的列表
  • #test_字典中的列表.yaml
    name:
     - 吴彦祖
     - 周星驰
     - uzi
    age: 20
    #test.py
    import yaml
    with open('test_字典中的列表.yaml',encoding='utf-8') as file1:
        data = yaml.load(file1,Loader=yaml.FullLoader)#读取yaml文件
        print(data)
    {'name': ['吴彦祖', '周星驰', 'uzi'], 'age': 20}
    

    当一个yaml文件内有多组数据时,用三个-分隔, 读取时使用yaml.load_all

    例如同时有列表和字典: