YAMLデータフォーマット学習

4549 ワード

YAMLはYAML Ain’t Markp Language(YAML™)の略語です.YAMLは表記言語ではないという意味です.
Python使用にはPyYamlモジュールのインストールが必要です.
pip install pyyaml
書き方:
1、YAMLの大きさは書き込みに敏感である;2、字下げは階層関係を表し、字下げはスペースのみ使用でき、TABは使用できない;3、同じレベルで左揃え;
4、注釈行の文法だけがあります.
 
サポートされているデータフォーマット:
1、オブジェクトは、キーパッドのペアのデータを使用します.辞書、ハッシュ
2、配列、値のセットリスト.
3、定数、単一の値.文字列(str)、ブール値(book)、整数(int)、浮動小数点(float)、Null、時間(time)、日付(date)
 
構文:
 1、辞書のフォーマット、Key:value、valueの前にスペースを入れなければなりません.
webserver:

  ip: 192.168.1.1
  port: 8000
  create_date: 2019-06-09
  create_time:1:01:01:01


#   :
{ webserver: 
   { ip: '192.168.1.1',
     port: 8000,
     create_date: Sun Jun 09 2019 08:00:00 GMT+0800 (      ),
     create_time: 219661 } }
 
 
2、リスト形式、-value、-valueの前にスペースを入れなければなりません.
ip: 
  - 192.168.1.1
  - 192.168.1.2
  - 192.168.1.3
  - 192.168.1.4

#   :
{ ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] }
 
 
3、リストと辞書が入れ子になっています.
webserver:
  - ip: 
    - 192.168.1.1
    - 192.168.1.2
    - 192.168.1.3
    - 192.168.1.4
  - port: 8000
#        
#   :
{ webserver: 
   [ { ip: [ '192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4' ] },
     { port: 8000 } ] }
 
4、!!強制データタイプ変換
port: !!str 8000
num: !!int '1999'
boolean: !!bool 'true'
second: !!float '18.362'

# yaml ,     ,         ,   ,                ,    。
#   
{ port: '8000', num: 1999, boolean: true, second: 18.362 }
 
5、アンカーポイントの定義&参照、&和*は行頭としては使用できません.同じ値を繰り返し参照するのに役立ちます.
port: &webport  #     ,&      
  - 8001
  - 8002
server:
  ip: 192.168.1.1
  port: *webport    #   

public: 
 addr: &addr1 liaoning  #     


address:                #   
  - *addr1

  :
{ port: [ 8001, 8002 ],
  server: [ '192.168.1.1', [ 8001, 8002 ] ],
  public: null,
  addr: 'liaoning',
  address: [ 'liaoning' ] }
6、  統合、<
merge: 
  - &CENTER { x: 1, y: 2 } 
  - &LEFT { x: 0, y: 2 } 
  - &BIG { r: 10 } 
  - &SMALL { r: 1 }
sample1: 
  <<: sample2:="" sample3:=""> 
    


 

7、 ,> |

segment_enter: >               # >  ,           
 Mark set a major league
 home run record in 1998.
line_enter: |                # |  ,     ,       
 65 Home Runs
 0.278 Batting Average
#   :
{ segment_enter: 'Mark set a major league home run record in 1998.
',   line_enter: '65 Home Runs
0.278 Batting Average
' }
 
 
8、---コンテンツを のドキュメントに し、 の は2つのファイルに します.
---
port: &webport  #     ,&      
  - 8001
  - 8002
server:
  ip: 192.168.1.1
  port: *webport    #   
  
---
public: 
 addr: &addr1 liaoning  #     
address:                #   
  - *addr1
 
9、app (ネットワークから):
# Test using included Django test app
# First install python-django
# Then launch the app in another terminal by doing
#   cd testapp
#   python manage.py testserver test_data.json
# Once launched, tests can be executed via:
#   python resttest.py http://localhost:8000 miniapp-test.yaml
---
- config:
    - testset: "Tests using test app"
- test: # create entity
    - name: "Basic get"
    - url: "/api/person/"
- test: # create entity
    - name: "Get single person"
    - url: "/api/person/1/"
- test: # create entity
    - name: "Get single person"
    - url: "/api/person/1/"
    - method: 'DELETE'
- test: # create entity by PUT
    - name: "Create/update person"
    - url: "/api/person/1/"
    - method: "PUT"
    - body: '{"first_name": "Gaius","id": 1,"last_name": "Baltar","login": "gbaltar"}'
    - headers: {'Content-Type': 'application/json'}
- test: # create entity by POST
    - name: "Create person"
    - url: "/api/person/"
    - method: "POST"
    - body: '{"first_name": "Willim","last_name": "Adama","login": "theadmiral"}'
    - headers: {Content-Type: application/json}
 
 
 
 
 
 
 
 
 
:https://blog.51cto.com/yishi/2363600