[Tutor] simple text replace
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
> > is that all occurrences are replaced (even if they are part of another
> Could you search for the string ' word ' with the white spaces around it?
> that only replace words that are not part of another word?
> > Essentially, I would like to replace only the occurrence that is
> > 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?)
> Bing™ brings you maps, menus, and reviews organized in one place. Try it
> Tutor maillist - Tutor at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor