String Replace Problem...

andrea.gavana_at_agip.it
Date: 02/28/05


To: python-list@python.org
Date: Mon, 28 Feb 2005 16:45:46 +0100

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/



Relevant Pages

  • Re: String Replace Problem...
    ... HTH ... > # Open And Read The File With Keywords ... > onread = fid.read ... > for keys in keywords: ...
    (comp.lang.python)
  • Re: Best way to check is a string is in a list?
    ... the keywords into an array, sort the array, and use Array.BinarySearch. ... "Gerrit Beuze" wrote: ... >> give you what you want for the case-insensitive keys. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: String Replace Problem...
    ... > # Open And Read The File With Keywords ... > onread = fid.read ... > for keys in keywords: ... > Andrea. ...
    (comp.lang.python)
  • Join Tables - Data Entry
    ... keywords that apply to various film titles. ... intermediate table called 'Occurrence' that contains foreign keys from ... But what if you don't have a lexicon of keywords already established ...
    (comp.databases.filemaker)