1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
---
title: Parsing strings (Str)
---
StrictYAML parses to a YAML object, not
the value directly to give you more flexibility
and control over what you can do with the YAML.
This is what that can object can do - in most
cases if parsed as a string, it will behave in
the same way.
Example yaml_snippet:
```yaml
a: 1
b: yes
c: â string
d: |
multiline string
```
```python
from strictyaml import Str, Map, load
from ensure import Ensure
schema = Map({"a": Str(), "b": Str(), "c": Str(), "d": Str()})
parsed = load(yaml_snippet, schema)
```
Parses correctly:
```python
Ensure(parsed).equals(
{"a": "1", "b": "yes", "c": u"â string", "d": "multiline string\n"}
)
```
Dict lookup cast to string:
```python
Ensure(str(parsed["a"])).equals("1")
```
Dict lookup cast to int:
```python
Ensure(int(parsed["a"])).equals(1)
```
Dict lookup cast to bool impossible:
```python
bool(parsed["a"])
```
```python
:
Cannot cast 'YAML(1)' to bool.
Use bool(yamlobj.data) or bool(yamlobj.text) instead.
```
!!! note "Executable specification"
Documentation automatically generated from
<a href="https://github.com/crdoconnor/strictyaml/blob/master/hitch/story/scalar-string.story">scalar-string.story
storytests.
|