[Tutor] simple text replace

j booth j8ooth at gmail.com
Mon Jul 27 00:23:01 CEST 2009


oops, I mean that the script opens a file and splits it into words; Then it
takes each word and searches for an alternative online; with the alternative
now stored in a variable, it simply needs to be written back over the
original word in the same file.

at the moment this might solve the problem, but I need to make sure:

    for line in fileinput.FileInput("test_file.txt",inplace=1):
>         line = line.replace(original, new, 1)
>
> does this make more sense?

On Sun, Jul 26, 2009 at 6:05 PM, Che M <pine508 at hotmail.com> wrote:

>
> > I am scanning a text file and replacing words with alternatives. My
> difficulty
> > is that all occurrences are replaced (even if they are part of another
> word!)..
>
> Could you search for the string ' word ' with the white spaces around it?
> Shouldn't
> that only replace words that are not part of another word?
>
> > Essentially, I would like to replace only the occurrence that is
> currently
> > selected-- not the rest. for example:
>
> What do you mean by a word being "currently selected"?  That phrase and
> "scanning a text file" don't make sense to me together.  How is the user
> reading the text to select the word that is to be replaced?  (Or maybe I am
> not understanding something?)
>
> CM
>
> ------------------------------
> Bing™ brings you maps, menus, and reviews organized in one place. Try it
> now.<http://www.bing.com/search?q=restaurants&form=MLOGEN&publ=WLHMTAG&crea=TXT_MLOGEN_Local_Local_Restaurants_1x1>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090726/7b18f9a2/attachment.htm>


More information about the Tutor mailing list