[Tutor] simple text replace

2009-07-26 Thread j booth
Hello,

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!)..

This is an example of what I have been using:

for line in fileinput.FileInput("test_file.txt",inplace=1):
> line = line.replace(original, new)
> print line,
> fileinput.close()


original and new are variables that have string values from functions..
original finds each word in a text file and old is a manipulated
replacement. Essentially, I would like to replace only the occurrence that
is currently selected-- not the rest. for example:

python is great, but my python knowledge is limited! regardless, I enjoy
> pythonprogramming


returns something like:

snake is great, but my snake knowledge is limited! regardless, I enjoy
> snakeprogramming


thanks so much!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simple text replace

2009-07-26 Thread j booth
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  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.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] word replace

2009-08-30 Thread j booth
Hello,

I have been experiencing such difficulty with something that should be
easy-- pretty new to programming..

Need to:
(1) open a text file
(2) iterate through it word by word
(3) replace each word with an alternative word and close the file.

**I don't know what the alternative word will be before running the
script...

this looks sloppy, but for brevity I'll ignore the rest of the code that is
working and post the area I'm having trouble with;

def exchange():
>
> original = old() # 'old()' parses the txt file and iterates through
> each of the words
> new = syn() # the processed alternative that I want to replace the
> original with
>
> try:
>
> for line in fileinput.input("test.txt",inplace=1):
> line = line.replace(original, new, 1)
> print line,
>
>
> finally:
> fileinput.close()
>

So, I'm aware that the in the definition 'exchange()' I don't actually parse
the file test again.. the result is that it seems to loop the entire file
everytime it replaces a word; consequently, if there is a word at in the
last paragraph of the file it is also replaced anywhere proceeding that
point. For example:

this is a test for testing sake


will give the result:

this is a trial for testing sake


but...

this is a test for testing sake
> test


will give the result:

this is a trial for trialing sake
> trial
>


Any help will be greatly appreciated! I've been quite slow in figuring out
this problem.. which is funny as I've been able to solve several other
problems that seem as if they should be much more complicated..

thanks for the time..
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor