学习pyyaml

网上查了一圈,觉得较好的yaml教程有:

YAML 语言教程 :http://www.ruanyifeng.com/blog/2016/07/yaml.html。

另外,在github的pyyaml库中找到一个示例

 

  1 #
  2 # Examples from the Preview section of the YAML specification
  3 # (http://yaml.org/spec/1.2/#Preview)
  4 #
  5 
  6 # Sequence of scalars 数组
  7 ---
  8 - Mark McGwire
  9 - Sammy Sosa
 10 - Ken Griffey
 11 
 12 # Mapping scalars to scalars 字典
 13 ---
 14 hr:  65    # Home runs
 15 avg: 0.278 # Batting average
 16 rbi: 147   # Runs Batted In
 17 
 18 # Mapping scalars to sequences 键为字符串,值为数组的字典
 19 ---
 20 american:
 21   - Boston Red Sox
 22   - Detroit Tigers
 23   - New York Yankees
 24 national:
 25   - New York Mets
 26   - Chicago Cubs
 27   - Atlanta Braves
 28 
 29 # Sequence of mappings 项为字典的数组
 30 ---
 31 -
 32   name: Mark McGwire
 33   hr:   65
 34   avg:  0.278
 35 -
 36   name: Sammy Sosa
 37   hr:   63
 38   avg:  0.288
 39 
 40 # Sequence of sequences 项为数组的数组
 41 ---
 42 - [name        , hr, avg  ]
 43 - [Mark McGwire, 65, 0.278]
 44 - [Sammy Sosa  , 63, 0.288]
 45 
 46 # Mapping of mappings 字典套构
 47 ---
 48 Mark McGwire: {hr: 65, avg: 0.278}
 49 Sammy Sosa: {
 50     hr: 63,
 51     avg: 0.288
 52   }
 53 
 54 # Two documents in a stream
 55 --- # Ranking of 1998 home runs
 56 - Mark McGwire
 57 - Sammy Sosa
 58 - Ken Griffey
 59 --- # Team ranking
 60 - Chicago Cubs
 61 - St Louis Cardinals
 62 
 63 # Documents with the end indicator
 64 ---
 65 time: 20:03:20
 66 player: Sammy Sosa
 67 action: strike (miss)
 68 ...
 69 ---
 70 time: 20:03:47
 71 player: Sammy Sosa
 72 action: grand slam
 73 ...
 74 
 75 # Comments
 76 ---
 77 hr: # 1998 hr ranking
 78   - Mark McGwire
 79   - Sammy Sosa
 80 rbi:
 81   # 1998 rbi ranking
 82   - Sammy Sosa
 83   - Ken Griffey
 84 
 85 # Anchors and aliases
 86 ---
 87 hr:
 88   - Mark McGwire
 89   # Following node labeled SS
 90   - &SS Sammy Sosa
 91 rbi:
 92   - *SS # Subsequent occurrence
 93   - Ken Griffey
 94 
 95 # Mapping between sequences
 96 ---
 97 ? - Detroit Tigers
 98   - Chicago cubs
 99 :
100   - 2001-07-23
101 ? [ New York Yankees,
102     Atlanta Braves ]
103 : [ 2001-07-02, 2001-08-12,
104     2001-08-14 ]
105 
106 # Inline nested mapping
107 ---
108 # products purchased
109 - item    : Super Hoop
110   quantity: 1
111 - item    : Basketball
112   quantity: 4
113 - item    : Big Shoes
114   quantity: 1
115 
116 # Literal scalars
117 --- | # ASCII art
118   \//||\/||
119   // ||  ||__
120 
121 # Folded scalars
122 --- >
123   Mark McGwire's
124   year was crippled
125   by a knee injury.
126 
127 # Preserved indented block in a folded scalar
128 ---
129 >
130  Sammy Sosa completed another
131  fine season with great stats.
132 
133    63 Home Runs
134    0.288 Batting Average
135 
136  What a year!
137 
138 # Indentation determines scope
139 ---
140 name: Mark McGwire
141 accomplishment: >
142   Mark set a major league
143   home run record in 1998.
144 stats: |
145   65 Home Runs
146   0.278 Batting Average
147 # Quoted scalars
148 ---
149 unicode: "Sosa did fine.\u263A"
150 control: "\b1998\t1999\t2000\n"
151 hex esc: "\x0d\x0a is \r\n"
152 single: '"Howdy!" he cried.'
153 quoted: ' # not a ''comment''.'
154 tie-fighter: '|\-*-/|'
155 
156 # Multi-line flow scalars
157 ---
158 plain:
159   This unquoted scalar
160   spans many lines.
161 quoted: "So does this
162   quoted scalar.\n"
163 
164 # Integers
165 ---
166 canonical: 12345
167 decimal: +12_345
168 sexagesimal: 3:25:45
169 octal: 014
170 hexadecimal: 0xC
171 
172 # Floating point
173 ---
174 canonical: 1.23015e+3
175 exponential: 12.3015e+02
176 sexagesimal: 20:30.15
177 fixed: 1_230.15
178 negative infinity: -.inf
179 not a number: .NaN
180 
181 # Miscellaneous
182 ---
183 null: ~
184 true: boolean
185 false: boolean
186 string: '12345'
187 
188 # Timestamps
189 ---
190 canonical: 2001-12-15T02:59:43.1Z
191 iso8601: 2001-12-14t21:59:43.10-05:00
192 spaced: 2001-12-14 21:59:43.10 -5
193 date: 2002-12-14
194 
195 # Various explicit tags
196 ---
197 not-date: !!str 2002-04-28
198 picture: !!binary |
199  R0lGODlhDAAMAIQAAP//9/X
200  17unp5WZmZgAAAOfn515eXv
201  Pz7Y6OjuDg4J+fn5OTk6enp
202  56enmleECcgggoBADs=
203 application specific tag: !something |
204  The semantics of the tag
205  above may be different for
206  different documents.
207 # Global tags
208 %TAG ! tag:clarkevans.com,2002:
209 --- !shape
210   # Use the ! handle for presenting
211   # tag:clarkevans.com,2002:circle
212 - !circle
213   center: &ORIGIN {x: 73, y: 129}
214   radius: 7
215 - !line
216   start: *ORIGIN
217   finish: { x: 89, y: 102 }
218 - !label
219   start: *ORIGIN
220   color: 0xFFEEBB
221   text: Pretty vector drawing.
222 
223 # Unordered sets
224 --- !!set
225 # sets are represented as a
226 # mapping where each key is
227 # associated with the empty string
228 ? Mark McGwire
229 ? Sammy Sosa
230 ? Ken Griff
231 
232 # Ordered mappings
233 --- !!omap
234 # ordered maps are represented as
235 # a sequence of mappings, with
236 # each mapping having one key
237 - Mark McGwire: 65
238 - Sammy Sosa: 63
239 - Ken Griffy: 58
240 
241 # Full length example
242 --- !<tag:clarkevans.com,2002:invoice>
243 invoice: 34843
244 date   : 2001-01-23
245 bill-to: &id001
246     given  : Chris
247     family : Dumars
248     address:
249         lines: |
250             458 Walkman Dr.
251             Suite #292
252         city    : Royal Oak
253         state   : MI
254         postal  : 48046
255 ship-to: *id001
256 product:
257     - sku         : BL394D
258       quantity    : 4
259       description : Basketball
260       price       : 450.00
261     - sku         : BL4438H
262       quantity    : 1
263       description : Super Hoop
264       price       : 2392.00
265 tax  : 251.42
266 total: 4443.52
267 comments:
268     Late afternoon is best.
269     Backup contact is Nancy
270     Billsmer @ 338-4338.
271 
272 # Another full-length example
273 ---
274 Time: 2001-11-23 15:01:42 -5
275 User: ed
276 Warning:
277   This is an error message
278   for the log file
279 ---
280 Time: 2001-11-23 15:02:31 -5
281 User: ed
282 Warning:
283   A slightly different error
284   message.
285 ---
286 Date: 2001-11-23 15:03:17 -5
287 User: ed
288 Fatal:
289   Unknown variable "bar"
290 Stack:
291   - file: TopClass.py
292     line: 23
293     code: |
294       x = MoreObject("345\n")
295   - file: MoreClass.py
296     line: 58
297     code: |-
298       foo = bar

 

英文不太好,很多不太会翻译。就不误导别人了,见谅。

 

猜你喜欢

转载自www.cnblogs.com/lyg-blog/p/10122996.html