| | 20 | '''Warning: It is not safe to call `yaml.load` with any data received from an untrusted source! |
| | 21 | `yaml.load` is as powerful as `pickle.load` and so may call any Python function.''' |
| | 22 | Check the '''`yaml.safe_load`''' function though. |
| | 23 | |
| | 24 | The function '''`yaml.load`''' converts a YAML document to a Python object. |
| | 25 | |
| | 26 | {{{ |
| | 27 | #!python |
| | 28 | >>> yaml.load(""" |
| | 29 | ... - Hesperiidae |
| | 30 | ... - Papilionidae |
| | 31 | ... - Apatelodidae |
| | 32 | ... - Epiplemidae |
| | 33 | ... """) |
| | 34 | |
| | 35 | ['Hesperiidae', 'Papilionidae', 'Apatelodidae', 'Epiplemidae'] |
| | 36 | }}} |
| | 37 | |
| | 38 | '''`yaml.load`''' accepts a string, a Unicode string, an open file object, or |
| | 39 | an open Unicode file object. A string or a file must be encoded with '''utf-8''', |
| | 40 | '''utf-16-be''' or '''utf-16-le''' encoding. '''`yaml.load`''' detects the encoding |
| | 41 | by checking the '''BOM''' (byte order mark) sequence at the beginning of the |
| | 42 | string/file. If no '''BOM''' is present, the '''utf-8''' encoding is assumed. |
| | 43 | {{{ |
| | 44 | #!python |
| | 45 | >>> yaml.load(u""" |
| | 46 | ... hello: Привет! |
| | 47 | ... """) |
| | 48 | |
| | 49 | {'hello': u'\u041f\u0440\u0438\u0432\u0435\u0442!'} |
| | 50 | |
| | 51 | >>> stream = file('document.yaml', 'r') # 'document.yaml' contains a single YAML document. |
| | 52 | >>> yaml.load(stream) |
| | 53 | [...] # A Python object corresponding to the document. |
| | 54 | }}} |
| | 55 | |
| | 56 | if a string or a file contains several documents, you may load them all with the |
| | 57 | '''`yaml.load_all`''' function. |
| | 58 | |
| | 59 | {{{ |
| | 60 | #!python |
| | 61 | >>> documents = """ |
| | 62 | ... --- |
| | 63 | ... name: The Set of Gauntlets 'Pauraegen' |
| | 64 | ... description: > |
| | 65 | ... A set of handgear with sparks that crackle |
| | 66 | ... across its knuckleguards. |
| | 67 | ... --- |
| | 68 | ... name: The Set of Gauntlets 'Paurnen' |
| | 69 | ... description: > |
| | 70 | ... A set of gauntlets that gives off a foul, |
| | 71 | ... acrid odour yet remains untarnished. |
| | 72 | ... --- |
| | 73 | ... name: The Set of Gauntlets 'Paurnimmen' |
| | 74 | ... description: > |
| | 75 | ... A set of handgear, freezing with unnatural cold. |
| | 76 | ... """ |
| | 77 | |
| | 78 | >>> for data in yaml.load_all(documents): |
| | 79 | ... print data |
| | 80 | |
| | 81 | {'description': 'A set of handgear with sparks that crackle across its knuckleguards.\n', |
| | 82 | 'name': "The Set of Gauntlets 'Pauraegen'"} |
| | 83 | {'description': 'A set of gauntlets that gives off a foul, acrid odour yet remains untarnished.\n', |
| | 84 | 'name': "The Set of Gauntlets 'Paurnen'"} |
| | 85 | {'description': 'A set of handgear, freezing with unnatural cold.\n', |
| | 86 | 'name': "The Set of Gauntlets 'Paurnimmen'"} |
| | 87 | }}} |
| | 88 | |