I'm having troubles with textwrap.dedent
from the Python standard library. I tried to solve the issue described here: How to remove extra indentation of Python triple quoted multi-line strings?, but for an XML like string and it's not working as expected.
help(textwrap.dedent)
Help on function dedent in module textwrap:
dedent(text)
Remove any common leading whitespace from every line in `text`.
This can be used to make triple-quoted strings line up with the left
edge of the display, while still presenting them in the source code
in indented form.
Note that tabs and spaces are both treated as whitespace, but they
are not equal: the lines " hello" and "\thello" are
considered to have no common leading whitespace. (This behaviour is
new in Python 2.5; older versions of this module incorrectly
expanded tabs before searching for common leading whitespace.)
I'm on Python 3.6.9 / Ubuntu 18.04.
Here's an example:
template_string = """\
<?xml version="1.0" encoding="utf-8" ?>
<doc>
<param1>hello</param1>
<param1>world</param1>
</doc>
"""
then:
>>> textwrap.dedent(template_string)
'<?xml version="1.0" encoding="utf-8" ?>\n <xml>\n <param1>hello</param1>\n <param1>world</param1>\n </xml>\n'
I still have extra spaces.
Expected output is as follow (I know I have to also apply .replace('\n','')
afterwards):
'<?xml version="1.0" encoding="utf-8" ?><xml>\<param1>hello</param1><param1>world</param1></xml>'
The idea is to end with with a single-line XML in order to write this to a postgres database