0

I am trying to save to a txt a multi-line string without the newline and any multiple spaces:

with open('test.txt','w') as f:    
f.write( r"""<rect
       x="0"
       y="0"
       width="%s"
       height="%s"
       stroke="red"
       stroke-width="1"
       fill-opacity="0"  />""" %(3,4)+"\n" )

When I open the file cat 'text.txt'the file is on multiple lines. How can I have the code written in a single line without multiple white spaces?

<rect x="0" y="0" width="3" height="4" stroke="red" stroke-width="1" fill-opacity="0" />

Without using for instance "".join() or other methods which will affect the readability of the code?

Using .replace('\n', '') will not delete the multiple white space.

G M
  • 20,759
  • 10
  • 81
  • 84

2 Answers2

1

Add .replace('\n', '') at the end of the string.

Edited: And by "at the end of the string", I mean the multi-line one, e.g.:

f.write( r"""<rect
       x="0"
       y="0"
       width="%s"
       height="%s"
       stroke="red"
       stroke-width="1"
       fill-opacity="0"  />""".replace('\n', '') %(3,4)+"\n" )
                              ^^^^^^^^^^^^^^^^^^
                                     HERE

Another possibility is to benefit from Python's automatic concatenation of strings, as follows:

f.write('<rect '
        'x="0" '
        'y="0" '
        'width="%s" '
        'height="%s" '
        'stroke="red" '
        'stroke-width="1" '
        'fill-opacity="0"/> ' % (3,4) + '\n')
Guybrush
  • 2,680
  • 1
  • 10
  • 17
  • Sorry I had an error becasue I put the operator in the wrong place , now it seems it works but I have all the white spaces – G M Mar 29 '19 at 10:09
  • Substitute '\n' with '\n ' in that case, or don't put white spaces in the multiline strings! – Guybrush Mar 29 '19 at 10:10
  • @GM I updated my answer to provide an alternative way to deal with whitespaces and newlines. If it solves your problem, could you mark my answer? – Guybrush Mar 29 '19 at 15:37
  • @Guybrush Thanks a lot! I usually wait some days to leave the opportunity for other users to answer! – G M Mar 29 '19 at 17:05
  • @gm if the problem is solved, why wasting other users' time? ;-) – Guybrush Mar 29 '19 at 17:41
0

I'm guessing that you want all the code in 1 line for the multi line string?

then just do it like this

f.write(r"""<rect x="0" y="0" width="%s"

ect...

That way you are putting everything on one line

Hope this helps

  • Thanks I know that, but if I write all in one line I will exceed the maximum line length of 79 characters indicated in PEP. So this doens't answer to my question – G M Mar 29 '19 at 10:33
  • Are you sure this code of your's is above the maximum line 79 characters? because it worked for me perfectly fine with no issues. – Tazza Bazza Mar 29 '19 at 10:39
  • Sorry i'm getting really confused on what you are saying about PEP not rising any errors? for me i got no errors at all – Tazza Bazza Mar 29 '19 at 11:22
  • PEP is a guideline to follow https://www.python.org/dev/peps/pep-0008/#id19 so I am sure your solution will work but it doesn't follow the guideline either answer to my question – G M Mar 29 '19 at 12:54