The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Python remove enter / space between lines
Discuss Python remove enter / space between lines in the Python Programming forum on Dev Shed. Python remove enter / space between lines Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 14th, 2012, 01:45 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 14 m 10 sec
Reputation 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)
|

November 14th, 2012, 07:56 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
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.
|

November 14th, 2012, 08:08 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
Maybe even just..
Code:
print >>opennew, "%s%s" % (word1,line[30:-2])
which chops of the last character. Etc..
|

November 14th, 2012, 08:34 AM
|
 |
Contributing User
|
|
|
|
|
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!
|

November 14th, 2012, 10:05 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 14 m 10 sec
Reputation 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
Quote: | Originally Posted by AndrewSW Maybe even just..
Code:
print >>opennew, "%s%s" % (word1,line[30:-2])
which chops of the last character. Etc.. |
|

November 14th, 2012, 10:19 AM
|
 |
JavaScript is not spelt java
|
|
Join Date: Feb 2011
Location: Landan, England
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|