Open lhotakj opened 4 years ago
This is a real pain. Glad I found this issue to help me understand what was going on :) Would be great to get it fixed up.
From this question https://stackoverflow.com/questions/71836675/force-pyyaml-to-write-multiline-string-literals-regardless-of-string-content, I moved to import ruamel.yaml as yaml
and it dumped all strings correctly as required by content_ok on the first post by using default_style='|'
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# python3 -m pip install ruamel.yaml
import ruamel.yaml as yaml
raw_yaml = """
---
- weird parsing:
content_ok: |
this is OK
without space
content_bad: |
this is not ok
as it has a trailing space
"""
s = yaml.safe_load(raw_yaml)
print(yaml.safe_dump_all(s, default_flow_style=False, default_style='|'))
Result:
"weird parsing":
"content_bad": |
this is not ok
as it has a trailing space
"content_ok": |
this is OK
without space
With print("%s" % repr(yaml.safe_dump_all(s, default_flow_style=False, default_style='|')))
: '"weird parsing":\n "content_bad": |\n this is not ok\n as it has a trailing space \n "content_ok": |\n this is OK\n without space\n'
Up
+1
Hi, this issue is related to issue #402.
Python 3.7 gives you the following output:
Note that
content_bad
is formatted differently thancontent_ok
just because of a traling space. This small issue caused me a lot of headaches ;) Would it be possible to makedump
method working consistently? Thanks a lot