String Replace Problem...

wes weston wweston at att.net
Mon Feb 28 11:37:10 EST 2005


andrea.gavana at agip.it wrote:
> Hello NG,
> 
>       probably this is a basic question, but I'm going crazy... I am unable
> to find an answer. Suppose that I have a file (that I called "Errors.txt")
> which contains these lines:
> 
> MULTIPLY
>   'PERMX'  @PERMX1  1 34  1  20  1 6     /
>   'PERMX'  @PERMX2  1 34  21 41  1 6     /
>   'PERMX'  @PERMX3  1 34  1  20  7 14    /
>   'PERMX'  @PERMX4  1 34  21 41  7 14    /
>   'PERMX'  @PERMX5  1 34  1  20 15 26    /
>   'PERMX'  @PERMX6  1 34  21 41 15 26    /
>   'PERMX'  @PERMX7  1 34  1  20 27 28    /
>   'PERMX'  @PERMX8  1 34  21 41 27 28    /
>   'PERMX'  @PERMX9  1 34  1  20 29 34    /
>   'PERMX'  @PERMX10  1 34  21 41 29 34    /
>   'PERMX'  @PERMX11  1 34  1  20 35 42    /
>   'PERMX'  @PERMX12  1 34  21 41 35 42    /
>   'PERMX'  @PERMX13  1 34  1  20 43 53    /
>   'PERMX'  @PERMX14  1 34  21 41 43 53    /
>   'PERMX'  @PERMX15  1 34  1  20 54 61    /
>   'PERMX'  @PERMX16  1 34  21 41 54 61    /
> /
> 
> I would like to replace all the occurrencies of the "keywords" (beginning
> with the @ (AT) symbol) with some floating point value. As an example, this
> is what I do:
> 
> #  --- CODE BEGIN
> 
> import re
> import string
> 
> # Set Some Dummy Parameter Values
> parametervalues = range(1, 17)
> 
> # Open And Read The File With Keywords
> fid = open("Errors.txt","rt")
> onread = fid.read()
> fid.close()
> 
> # Find All Keywords Starting with @ (AT)
> regex = re.compile("[\@]\w+", re.IGNORECASE)
> keywords = regex.findall(onread)
> 
> counter = 0
> 
> # Try To Replace The With Floats
> for keys in keywords:
>     pars = parametervalues[counter]
>     onread = string.replace(onread, keys, str(float(pars)))
>     counter = counter + 1
> 
> # Write A New File With Replaced Values
> fid = open("Errors_2.txt","wt")
> fid.write(onread)
> fid.close()
> 
> #  --- CODE END
> 
> 
> Now, I you try to run this little script, you will see that for keywords
> starting from "@PERMX10", the replaced values are WRONG. I don't know why,
> Python replace only the "@PERMX1" leaving out the last char of the keyword
> (that are 0, 1, 2, 3, 4, 5, 6 ). These values are left in the file and I
> don't get the expected result.
> 
> Does anyone have an explanation? What am I doing wrong?
> 
> Thanks to you all for your help.
> 
> Andrea.
> 
> ------------------------------------------------------------------------------------------------------------------------------------------
>  Message for the recipient only, if received in error, please notify the
> sender and read http://www.eni.it/disclaimer/
> 
> 

andrea,
    If you put in "keywords.reverse()" after getting
keywords, things may work better. Though not a good
fix, it illustrates part of the problem. If you replace
"@PERMX1" you also replace part of "@PERMX10".
    Wouldn't it be better to read the file as lines
instead of strings?
wes




More information about the Python-list mailing list