#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    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)
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    print will add a newline but each line must already have a newline character at the end - hence the blank lines.

    You could use write() but this requires a string:

    Code:
    opennew.write(str(word1)+ " " + line[30:])
    This looks messy to me though - converting to an integer and then back to a string! There are many other ways to do this, including printf, string interpolation, combined with using rstrip() to remove the newline characters, etc..

    Edited: Actually, Python doesn't have printf natively, but Python 2.6 includes str.format().
    Last edited by AndrewSW; November 14th, 2012 at 08:02 AM.
  4. #3
  5. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Maybe even just..

    Code:
    print >>opennew, "%s%s" % (word1,line[30:-2])
    which chops of the last character. Etc..
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    If you still haven't got an workable answer please show a small input file and the result you'd like to see.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Solved


    Thanks Andrew, the "-2" fixed the problem.
    I was also able to fix it by changing the "print" line by:

    opennew.write (str(word1) + (line[30:]) + "")

    The additional quotes at the end are the ones that eliminate that additional (unwanted) enter/space. It worked for me, but honestly I do not know why, do you know what those additional quotes do?

    Thanks


    Originally Posted by AndrewSW
    Maybe even just..

    Code:
    print >>opennew, "%s%s" % (word1,line[30:-2])
    which chops of the last character. Etc..
  10. #6
  11. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    opennew.write (str(word1) + (line[30:]) + "")

    I can't see why you would need the additional quotes. Perhaps try

    print line[30:]

    to see what shows in the console.

IMN logo majestic logo threadwatch logo seochat tools logo