There are several ways around this, one is to transform the output:
import sys
import ruamel.yaml
def strip_document_end_marker(s):
if s.endswith('...\n'):
return s[: -4]
yaml = ruamel.yaml.YAML()
yaml.dump("abc", sys.stdout, transform = strip_document_end_marker)
which gives:
abc
Another way to achieve this is to reset the open_ended
attribute after writing a plain value:
import sys
import ruamel.yaml
yaml = ruamel.yaml.YAML()
def wp(self, * args, ** kw):
self.write_plain_org( * args, ** kw)
self.open_ended = False
yaml.Emitter.write_plain_org = yaml.Emitter.write_plain
yaml.Emitter.write_plain = wp
yaml.dump("abc", sys.stdout)
It seems that the serializer appends the three dots (document's end marker) is append when the Serializer get a non-iterable value:
dump(1, stream = sys.stdout)
dump([1], stream = sys.stdout)
dump(datetime.datetime.now(), stream = sys.stdout)
dump("1", stream = sys.stdout)
Prints:
1
...[1]
2019 - 07 - 09 12: 45: 27.013202
...
'1'
I am wondering if this is really bug or anycodings_yaml intention, but anyway. ,Another way to achieve this is to reset anycodings_ruamel.yaml the open_ended attribute after writing a anycodings_ruamel.yaml plain value:,Why dumping single value always includes anycodings_yaml explicit YAML end?,So an easy workaround would be to anycodings_ruamel.yaml convert your values to string before anycodings_ruamel.yaml dumping them (if possible)...
Why dumping single value always includes anycodings_yaml explicit YAML end?
import sys
from ruamel.yaml
import YAML
yaml = YAML()
yaml.explicit_end = False
yaml.dump(1, sys.stdout)
Produces
1 ...
There are several ways around this, one anycodings_ruamel.yaml is to transform the output:
import sys
import ruamel.yaml
def strip_document_end_marker(s):
if s.endswith('...\n'):
return s[: -4]
yaml = ruamel.yaml.YAML()
yaml.dump("abc", sys.stdout, transform = strip_document_end_marker)
which gives:
abc
Another way to achieve this is to reset anycodings_ruamel.yaml the open_ended attribute after writing a anycodings_ruamel.yaml plain value:
import sys
import ruamel.yaml
yaml = ruamel.yaml.YAML()
def wp(self, * args, ** kw):
self.write_plain_org( * args, ** kw)
self.open_ended = False
yaml.Emitter.write_plain_org = yaml.Emitter.write_plain
yaml.Emitter.write_plain = wp
yaml.dump("abc", sys.stdout)
It seems that the serializer appends the anycodings_ruamel.yaml three dots (document's end marker) is anycodings_ruamel.yaml append when the Serializer get a anycodings_ruamel.yaml non-iterable value:
dump(1, stream = sys.stdout)
dump([1], stream = sys.stdout)
dump(datetime.datetime.now(), stream = sys.stdout)
dump("1", stream = sys.stdout)
Prints:
1
...[1]
2019 - 07 - 09 12: 45: 27.013202
...
'1'
A python object can be marked as safe and thus be recognized by yaml.safe_load. To do this, derive it from yaml.YAMLObject (as explained in section Constructors, representers, resolvers) and explicitly set its class property yaml_loader to yaml.SafeLoader.,In order to represent static Python objects like functions or classes, you need to use a complex !!python/name tag. For instance, the function yaml.dump can be represented as,yaml.YAMLObject uses metaclass magic to register a constructor, which transforms a YAML node to a class instance, and a representer, which serializes a class instance to a YAML node.,Subclassing YAMLObject is an easy way to define tags, constructors, and representers for your classes. You only need to override the yaml_tag attribute. If you want to define your custom constructor and representer, redefine the from_yaml and to_yaml method correspondingly.
from yaml
import load, dump
try:
from yaml
import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml
import Loader, Dumper
#...
data = load(stream, Loader = Loader)
#...
output = dump(data, Dumper = Dumper)
import yaml document = "" " a: 1 b: c: 3 d: 4 "" " print yaml.dump(yaml.load(document))
>>> print yaml.dump(yaml.load(document), default_flow_style = False)
a: 1
b:
c: 3
d: 4
>>>
import yaml
>>> yaml.load(""
"
...-Hesperiidae
...-Papilionidae
...-Apatelodidae
...-Epiplemidae
...""
")
['Hesperiidae', 'Papilionidae', 'Apatelodidae', 'Epiplemidae']
>>> yaml.load(u "" " ...hello: Привет! ..."" ") # In Python 3, do not use the 'u' prefix { 'hello': u '\u041f\u0440\u0438\u0432\u0435\u0442!' } >>> stream = file('document.yaml', 'r') # 'document.yaml' contains a single YAML document. >>> yaml.load(stream)[...] # A Python object corresponding to the document.
Indentation of block sequences Inconsistently indented YAML ,Although ruamel.yaml doesn’t preserve individual indentations of block sequence items, it does properly dump:,adding/replacing comments on block-style sequences and mappings with smart column positioning,Inconsistently indented YAML
from ruamel.yaml
import YAML
yaml = YAML()
x: -b: 1 - 2
x: -b: 1 - 2
x: -b: 1 - 2
x: y: -b: 1 - 2
a:
b:
c: 1