Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a somewhat different answer here. dbaupp's answer is correct if the order of elements is important to you for reasons other than readability. If the only reason you want question to show up before answer is to make the file more human-readable, then you don't need to use !!omap, and can instead use custom representers to get the order you want. </p> <p>First of all, your problem with the dumper dumping without the - in front is because you're only dumping a single mapping, instead of a list of them. Put your dict inside a list and this will be fixed. So we start with:</p> <pre><code>d = [{"question": "What is the name of this sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...", "answer": ["The Fibonacci Sequence", "The Padovan Sequence", "The Morris Sequence"]}] </code></pre> <p>Now we have a particular order we want the output to be, so we'll specify that, and convert to OrderedDict with that order:</p> <pre><code>from collections import OrderedDict order = ['question', 'answer'] do = [ OrderedDict( sorted( z.items(), key=lambda x: order.index(x[0]) ) ) for z in d ] </code></pre> <p>Next, we need to make it so that PyYAML knows what to do with an OrderedDict. In this case, we don't want it to be an !!omap, we just want a mapping with a particular order. For some motivation unclear to me, if you give dumper.represent_mapping a dict, or anything with an items attribute, it will sort the items before dumping, but if you give it the output of items() (eg, a list of (key, value) tuples), it won't. Thus we can use</p> <pre><code>def order_rep(dumper, data): return dumper.represent_mapping( u'tag:yaml.org,2002:map', data.items(), flow_style=False ) yaml.add_representer( OrderedDict, order_rep ) </code></pre> <p>And then, our output from <code>print yaml.dump(do)</code> ends up as:</p> <pre class="lang-yaml prettyprint-override"><code>- question: 'What is the name of this sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...' answer: [The Fibonacci Sequence, The Padovan Sequence, The Morris Sequence] </code></pre> <p>There are a number of different ways this could be done. Using OrderedDict isn't actually necessary at all, you just need the question/answer pairs to be of some class that you can write a representer for.</p> <p>And again, do realize that this is only for human readability and aesthetic purposes. The order here will not be of any YAML significance, as it would if you were using !!omap. It just seemed like this was primarily important to you for readability.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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