Ticket #64 (closed task: fixed)
yaml.dump and indentation:
| Reported by: | lal.george@… | Owned by: | xi |
|---|---|---|---|
| Priority: | low | Component: | pyyaml |
| Severity: | normal | Keywords: | |
| Cc: |
Description
In python, when I have :
y = {'x' : 3, z : [{'val' : 3}, {'val' : 4}]} yaml.dump(y, default_flow_style=False)
The result is: 'x: 3\nz:\n- val: 3\n- val: 4\n'
I believe there should be a space before each hypen.
Best,
Lal
Attachments
Change History
comment:2 Changed 6 years ago by lageorge@…
- Status changed from closed to reopened
- Resolution invalid deleted
Unfortunately, I don't understand the notation very well; but l-block-seq-entry(n,c) ::= s-indent(seq-spaces(n,c)) "-" ...
Since a new block is being started, won't there be an s-indent?
Best,
Lal
p.s. The java version of yaml does not like the dump produced by PyYaml?.
comment:3 Changed 6 years ago by xi
- Status changed from reopened to closed
- Resolution set to invalid
Read the next paragraph after the production:
People read the “-” character as part of the indentation. Hence, block sequence entries require one less space of indentation, unless the block sequence is nested within another block sequence (hence the need for the block-in context and block-out context).
See also the example in the specification:
block: - one - ·- two
P.S. The YAML loader in Java must be broken then.
comment:4 Changed 6 years ago by anonymous
- Priority changed from highest to low
- Status changed from closed to reopened
- Resolution invalid deleted
- Type changed from defect to task
Thanks for the quick response ...
I believe you and PyYaml? are correct. The Java implementation is returning null for z in:
'x: 3\nz:\n- val: 3\n- val: 4\n'
Do you know a work around I could use? Perhaps I could turn this feature off, by always having PyYaml? emit " -" wherever it emits "-".
Thanks for your help.
Best,
Lal
comment:5 Changed 6 years ago by xi
- Status changed from reopened to closed
- Resolution set to fixed
You can use the following code:
import yaml
class MyDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
y = {'x' : 3, 'z' : [{'val' : 3}, {'val' : 4}]}
print yaml.dump(y, Dumper=MyDumper, default_flow_style=False)

The output is correct, see the production l-block-seq-entry(n,c) and Example 10.4:
http://yaml.org/spec/cvs/current.html#l-block-seq-entry(n,c)