> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of mastjeptor regli
> Sent: Wednesday, March 07, 2007 10:34 AM
> To: tutor@python.org
> Subject: [Tutor] help:find a string in a text and replace it 
> with a anotherstring
> 
> Hi,folks.
>      At first ,I have a question that I want to ask for 
> help,it is how can I search a specific string in a text (such 
> a in a word document) and replace it with another string?
>      Second,if the text is changed dynamicly,can I use a 
> variable string(has been assigned a string) to search in the 
> text and replace it with another variable string.for example, 
> Regex Substitution: s/email/e-mail ,if  the string of "email" 
> and "e-mail" are both changed from time to time,can we apply
> variable string (which has been assigned a value of string 
> type) instead of constant string of "email" and "e-mail" to 
> construct a regex substitution expression ?
>       Please reply with experience in using  regular 
> expression or python library functions to find and replace a 
> string in a text to help me.   Thank you for your attention.
> 

You might not need regular expressions. You can use replace method.

In [11]: x = "Bozo"

In [12]: z = "Bozo The Clown"

In [13]: y = "Krusty"

In [14]: z.replace(x,y)
Out[14]: 'Krusty The Clown'

You could probably populate a dictionary of the words you want to
replace and their replacements. Then use the replace method on the
strings.

Note that Word documents are binary gibberish, so you'd need to use
win32 python windows stuff making this more complicated. On plain text
files, it wouldn't be too bad.

Mike 
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to