Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 14th, 2012, 01:45 AM
hmartine54 hmartine54 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 hmartine54 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #2  
Old November 14th, 2012, 07:56 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
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.

Reply With Quote
  #3  
Old November 14th, 2012, 08:08 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
Maybe even just..

Code:
print >>opennew, "%s%s" % (word1,line[30:-2])


which chops of the last character. Etc..

Reply With Quote
  #4  
Old November 14th, 2012, 08:34 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
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!

Reply With Quote
  #5  
Old November 14th, 2012, 10:05 AM
hmartine54 hmartine54 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 hmartine54 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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..

Reply With Quote
  #6  
Old November 14th, 2012, 10:19 AM
AndrewSW's Avatar
AndrewSW AndrewSW is offline
JavaScript is not spelt java
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2011
Location: Landan, England
Posts: 743 AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level)AndrewSW User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 56 m
Reputation Power: 164
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python remove enter / space between lines

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap