0

When writing to a file whitespace is added between every line in the file. Tried using .strip() to remove the whitespace:

newstring = (mytemplate.render(identifiers=zipped_list))
print (newstring)

When the content is read into string newstring it will look like:

<headers>
        <universe>Default</universe>
        <domain>Instrument</domain>
        <universeOperation>Update</universeOperation>
        <acquisitionOperation>None</acquisitionOperation>
        <manufactureOperation>None</manufactureOperation>
        <publicationOperation>None</publicationOperation>
        <businessDate>2017-06-13</businessDate>
    </headers>
    <items>
        <item>
            <identifiers>
                <ID_BB_GLOBAL>TEST1234</ID_BB_GLOBAL>
            </identifiers>
            <classifiers>
                <CL_SUBSCRIBER>TEST</CL_SUBSCRIBER>
                <CL_STRATEGY>TEST</CL_STRATEGY>
            </classifiers>

When I write the string to a file:

file = open(FILE_PATH + "Test.xml", "w")
file.write(newstring)

it will look like this:

<headers>

        <universe>Default</universe>

        <domain>Instrument</domain>

        <universeOperation>Update</universeOperation>

        <acquisitionOperation>None</acquisitionOperation>

        <manufactureOperation>None</manufactureOperation>

        <publicationOperation>None</publicationOperation>

        <businessDate>2017-06-13</businessDate>

    </headers>

    <items>

        <item>

            <identifiers>

                <ID_BB_GLOBAL>BBG0016RLJ79</ID_BB_GLOBAL>

            </identifiers>

            <classifiers>

                <CL_SUBSCRIBER>SYS</CL_SUBSCRIBER>

                <CL_REMOVE>N</CL_REMOVE>

                <CL_STRATEGY>MAM_ID</CL_STRATEGY>

            </classifiers> 

How do I remove the whitespace between each line?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Seán Dempsey
  • 105
  • 3
  • 10

1 Answers1

0

.strip() removes the leading and tailing whitespaces of a string:

https://docs.python.org/3/library/stdtypes.html#str.strip

I wonder why your print() and your file.write() produce such different output since .write() actually does not append/modify newlines. Maybe its a Windows-thing (CRLF is standard on Windows)?

Anyway, in your case removing multiple newlines should remove the unwanted newlines:

your_string = re.sub(r'[\n\r]+', "\n", your_string, re.MULTILINE|re.DOTALL)

(you need to import re, of course.)

EDIT

I was curious and did some quick research after posting my answer and it indeed seemes to be a Windows-thing: on Windows, python automatically converts every newline to the sytem-specific representation, that is CRLF on Windows. To avoid this, one needs to specify how Python should handle newlines like:

file = open(your_file, newline="\n", ...)

So this could be a better solution (and a quicker one, too) See also:

https://docs.python.org/3/library/functions.html#open

meistermuh
  • 393
  • 3
  • 11
  • thanks @meistermuh this worked thanks for the help – Seán Dempsey Apr 26 '19 at 09:08
  • @SeánDempsey you are welcome. have a look at my updated answer, since there could be a better way to do it (I didn't test it but in theory it should work well, according to the documentation) – meistermuh Apr 26 '19 at 09:16