Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python remove enter / space between lines

OK so I have read a lot of the post that suggest how to eliminate that extra space, but for whatever reason it seems I can't apply those suggestions to my system, so I am here asking for your help.

These are the last few lines of my code:

    for line in rline[start+5 : end] :
        words = line.split()
        word1 = int(words[1])
        print >>opennew, "%s%s" % (word1,line[30:])

And the new "opennew" file looks like this :

    1        0.876153    0.152889   -0.047464

    2        1.011880   -1.161641   -2.096289

    3        0.883419    1.558736    1.966913

    4        2.010367   -1.140725    1.053368

While what I really want is:

    1        0.876153    0.152889   -0.047464
    2        1.011880   -1.161641   -2.096289
    3        0.883419    1.558736    1.966913
    4        2.010367   -1.140725    1.053368

Is there anyway I add to the code something to remove that additional (unwanted) enter/space from my input?

Thanks a lot for the help.

PS: (Please do not ask why I split the lines in the code, there is a purpose for this will have an application in a future (better) code)

like image 386
Justme Avatar asked Dec 09 '25 13:12

Justme


1 Answers

Try stripping the newlines off of line:

line = line.rstrip("\n")
like image 151
squiguy Avatar answered Dec 12 '25 08:12

squiguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!