Note that there are some explanatory texts on larger screens.

plurals
  1. POParse YAML and assume a certain path is always a string
    primarykey
    data
    text
    <p>I am using the YAML parser from <a href="http://pyyaml.org" rel="nofollow">http://pyyaml.org</a> and I want it to always interpret certain fields as string, but I can't figure out how add_path_resolver() works.</p> <p>For example: The parser assumes that "version" is a float:</p> <pre><code>network: - name: apple - name: orange version: 2.3 site: banana </code></pre> <p>Some files have "version: 2" (which is interpreted as an int) or "version: 2.3 alpha" (which is interpreted as a str).</p> <p>I want them to always be interpreted as a str.</p> <p>It seems that yaml.add_path_resolver() should let me specify, "When you see version:, always interpret it as a str) but it is not documented very well. My best guess is:</p> <pre><code>yaml.add_path_resolver(u'!root', ['version'], kind=str) </code></pre> <p>But that doesn't work. </p> <p>Suggestions on how to get my version field to always be a string?</p> <p>P.S. Here are some examples of different "version" strings and how they are interpreted:</p> <pre><code>(Pdb) import yaml (Pdb) import pprint (Pdb) pprint.pprint(yaml.load("---\nnetwork:\n- name: apple\n- name: orange\nversion: 2\nsite: banana")) {'network': [{'name': 'apple'}, {'name': 'orange'}], 'site': 'banana', 'version': 2} (Pdb) pprint.pprint(yaml.load("---\nnetwork:\n- name: apple\n- name: orange\nversion: 2.3\nsite: banana")) {'network': [{'name': 'apple'}, {'name': 'orange'}], 'site': 'banana', 'version': 2.2999999999999998} (Pdb) pprint.pprint(yaml.load("---\nnetwork:\n- name: apple\n- name: orange\nversion: 2.3 alpha\nsite: banana")) {'network': [{'name': 'apple'}, {'name': 'orange'}], 'site': 'banana', 'version': '2.3 alpha'} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload