YAML是一種直觀的能夠被電腦識別的的資料序列化格式,容易被人類閱讀,並且容易和指令碼語言互動。YAML類似於XML,但是文法比XML簡單得多,對於轉化成數組或可以hash的資料時是很簡單有效。
YAML文法規則:
http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/
http://www.yaml.org/
YAML被很多人認為是可以超越xml和json的檔案格式。對比xml,除了擁有xml的眾多優點外,它足夠簡單,便於使用。而對於json,YAML可以寫成正常化的設定檔(這我認為是高於json很多的優點,用json寫設定檔會讓人發瘋)。
YAML使用寄主語言的資料類型,這在多種語言中流傳的時候可能會引起相容性的問題。
如何寫yaml。(抄的) Yaml代碼 name: Tom Smith age: 37 spouse: name: Jane Smith age: 25 children: - name: Jimmy Smith age: 15 - name1: Jenny Smith age1: 12
具體文法請參照yaml文法規則。
--------------------------------------------------------------------------------------------
yaml在python上的具體實現:PyYaml
將yaml寫成配置指令碼test.yaml ,以下介紹如何讀寫yaml配置。
使用python的yaml庫PyYAML。http://pyyaml.org/
安裝到python lib下後就可以正常使用了。
Python代碼 #載入yaml import yaml #讀取檔案 f = open('test.yaml') #匯入 x = yaml.load(f) print x
也許你會得到以下類似的strings: Python代碼 {'age': 37, 'spouse': {'age': 25, 'name': 'Jane Smith'}, 'name': 'Tom Smith', 'children': [{'age': 15, 'name': 'Jimmy Smith'}, {'age1': 12, 'name1': 'Jenny Smith'}]}
python上使用yaml庫很簡單,基本就使用兩個函數:
yaml.load
yaml.dump
對於使用過pickle的各位童鞋來說,這意味著什麼不用詳說了吧。
Warning: It is not safe to call yaml.load with any data received from an untrusted source! yaml.load is as powerful as pickle.load and so may call any Python function.
對於yaml的讀取來講,最難的在於寫出正確的yaml資料格式。如果一不小心出錯,將會導致load異常,但有時沒有異常報,而是會讀不出任何資料。
pyYaml是完全的python實現,號稱比pickle更nb。(這誰知道呢。)
yaml.load accepts a byte string, a Unicode string, an open binary file object, or an open text file object. A byte string or a file must be encoded with utf-8, utf-16-be or utf-16-le encoding. yaml.load detects the encoding by checking the BOM (byte order mark) sequence at the beginning of the string/file. If no BOM is present, theutf-8 encoding is assumed.
yaml.load可接收一個byte字串,unicode字串,開啟的二進位檔案或文字檔對象。位元組字串和檔案必須是utf-8,utf-16-be或utf-16-le編碼的.yaml.load通過檢查字串/檔案開始的BOM(位元組序標記)來確認編碼。如果沒有BOM,就預設為utf-8。
百度上的關於BOM 在UCS 編碼中有一個叫做"ZERO WIDTH NO-BREAK SPACE"的字元,它的編碼是FEFF。而FFFE在UCS中是不存在的字元,所以不應該出現在實際傳輸中。UCS規範建議我們在傳輸位元組流前,先傳輸字元"ZERO WIDTH NO-BREAK SPACE"。這樣如果接收者收到FEFF,就表明這個位元組流是Big-Endian的;如果收到FFFE,就表明這個位元組流是Little- Endian的。因此字元"ZERO WIDTH NO-BREAK SPACE"又被稱作BOM。
UTF-8不需要BOM來表明位元組順序,但可以用BOM來表明編碼方式。字元"ZERO WIDTH NO-BREAK SPACE"的UTF-8編碼是EF BB BF。所以如果接收者收到以EF BB BF開頭的位元組流,就知道這是UTF-8編碼了。Windows就是使用BOM來標記文字檔的編碼方式的。
yaml.load 會返回一個python對象。關於會是什麼……看你資料是什麼了……
If a string or a file contains several documents, you may load them all with the yaml.load_all function.
如果string或檔案包含幾塊yaml文檔,你可以使用yaml.load_all來解析全部的文檔。
Python代碼 yaml.load(stream, Loader=<class 'yaml.loader.Loader'>) Parse the first YAML document in a stream #只解析第一個 and produce the corresponding Python object. yaml.load_all(stream, Loader=<class 'yaml.loader.Loader'>) Parse all YAML documents in a stream and produce corresponding Python objects.
yaml.load_all 會產生一個迭代器,你要做的就是for 讀出來
Python代碼 documents = """ name: The Set of Gauntlets 'Pauraegen' description: > A set of handgear with sparks that crackle across its knuckleguards. --- name: The Set of Gauntlets 'Paurnen' description: > A set of gauntlets that gives off a foul, acrid odour yet remains untarnished.