RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 366 | Replies: 5
Reply
Join Date: Sep 2008
Posts: 5
Reputation: blackcorner is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
blackcorner's Avatar
blackcorner blackcorner is offline Offline
Newbie Poster

Replace multiples substrings

  #1  
Sep 30th, 2008
Can anyone tell me if there is any function for replacing multiples substrings within a string.
For instance we have this function call to replace multiple appearances of the same substring:
  1. newLine = line.replace(';', '_')
Here I replace all appearances of ';' with '_'
But what I need is a function that receives a list of substrings to replace withing a string.
  1. newLine = line.replace([';', ',', '.'], '_')
Here I replace all appearances of ';', ',' and '.' with '_'

regards,
bc.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Durham, NC
Posts: 322
Reputation: jlm699 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 49
jlm699's Avatar
jlm699 jlm699 is offline Offline
Posting Whiz

Re: Replace multiples substrings

  #2  
Sep 30th, 2008
I don't know if there's a specific function to do it, but you could do a simple for loop like:
  1. >>> l = 'hi;your,face.is;on,fire'
  2. >>> for c in [';',',','.']:
  3. ... l = l.replace( c, '_' )
  4. ...
  5. >>> l
  6. 'hi_your_face_is_on_fire'
  7. >>>

Or roll your own little function ie,
  1. >>> l = 'hi;your,face.is;on,fire'
  2. >>> def replaces( inp, old, new ):
  3. ... for c in old: inp = inp.replace( c, new )
  4. ... return inp
  5. ...
  6. >>> l = replaces(l, ';,.', '_')
  7. >>> l
  8. 'hi_your_face_is_on_fire'
  9. >>>

Same function, using list instead of string of chars

  1. >>> l = 'hi;your,face.is;on,fire'
  2. >>> l = replaces(l, [ ';', ',', '.' ], '_')
  3. >>> l
  4. 'hi_your_face_is_on_fire'
  5. >>>
Last edited by jlm699 : Sep 30th, 2008 at 4:19 pm.
Let's Go Pens!

** Just because I reply to your question does not invite you to PM me. Keep discussions on the thread of topic, I will not answer your questions over PM. **
Reply With Quote  
Join Date: Oct 2007
Posts: 24
Reputation: jice is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
jice jice is offline Offline
Newbie Poster

Re: Replace multiples substrings

  #3  
Oct 1st, 2008
You can use regexp (re module) instead of string module for that.
Last edited by jice : Oct 1st, 2008 at 6:13 am.
Reply With Quote  
Join Date: Oct 2006
Posts: 1,669
Reputation: sneekula is on a distinguished road 
Rep Power: 6
Solved Threads: 47
sneekula's Avatar
sneekula sneekula is offline Offline
Posting Virtuoso

Re: Replace multiples substrings

  #4  
Oct 1st, 2008
If you don't mind learning regex, which is almost another language, but used by many popular computer languages for text manipulation:
  1. import re
  2.  
  3. p = re.compile(r'[;,.]')
  4. s = 'hi;your,face.is;on,fire'
  5. print p.sub('_', s) # hi_your_face_is_on_fire
No one died when Clinton lied.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Replace multiples substrings

  #5  
Oct 1st, 2008
Originally Posted by sneekula View Post
If you don't mind learning regex, which is almost another language

Regex it is just that, regular expression. A pattern describing some text.
Last edited by Aia : Oct 1st, 2008 at 3:00 pm.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Rep Power: 2
Solved Threads: 22
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: Replace multiples substrings

  #6  
Oct 1st, 2008
Originally Posted by Aia View Post
Regex it is just that, regular expression. A pattern describing some text.
Yes we know! How about learning some Python first before giving primitive lectures. We are trying to help people and keep a friendly atmosphere here!

BTW, if you need help with the module re, simply type help(re). Or go to this Python expert:
http://www.amk.ca/python/howto/regex/
Last edited by ZZucker : Oct 1st, 2008 at 5:49 pm.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:44 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC