943,608 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3210
  • Python RSS
Jul 1st, 2008
0

collapse white space in string

Expand Post »
I would like to collapse whitespace in a string of the followinf foramat using a some pre defined functionalities.

XXXXX<space>-<space>XXXX shoud be converted to XXXXX-XXXXX
XXXXX<space>-XXXXX shoud be converted to XXXXX-XXXXX

Please help
Similar Threads
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008
Jul 1st, 2008
0

Re: collapse white space in string

-
Last edited by mengqing; Jul 1st, 2008 at 10:35 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mengqing is offline Offline
5 posts
since May 2008
Jul 1st, 2008
0

Re: collapse white space in string

Something like this should do:
python Syntax (Toggle Plain Text)
  1. s = 'xxxxx - xxxxx'
  2. space = ' '
  3. nospace = ''
  4. s = s.replace(space, nospace)
  5. print s # xxxxx-xxxxx
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Jul 1st, 2008
1

Re: collapse white space in string

Python Syntax (Toggle Plain Text)
  1. >>> s = 'XXXXX -\n\t XXXX'
  2. >>> ''.join(s.split())
  3. 'XXXXX-XXXX'
  4. >>>
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Jul 3rd, 2008
0

Re: collapse white space in string

Thanks a lot. Its working now.
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: RE:Python path using Batch File
Next Thread in Python Forum Timeline: Need a python tFTP open source class library





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC