473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String Replace Problem...

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/
Jul 18 '05 #1
6 2234
an***********@agip.it wrote:
'PERMX' @PERMX1 1 34 1 20 1 6 / .... 'PERMX' @PERMX10 1 34 21 41 29 34 / ....
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:

# Find All Keywords Starting with @ (AT)
regex = re.compile("[\@]\w+", re.IGNORECASE)
You don't need the [, \ or ] around the "@" as it is
not a special character... (but that's not your problem here).
keywords = regex.findall(onread)
Here you get a list, in order, of all the matches, including
these: "@PERMX1" and "@PERMX10"
# Try To Replace The With Floats
for keys in keywords:
onread = string.replace(onread, keys, str(float(pars)))
Here you iterate through the list, replacing *all*
occurrences of each of them, one at a time, in the
full string.

Now imagine what happens to things like "@PERMX10" in the
full string when you are replacing "@PERMX1"....
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?


Basically, you are replacing things in the string one by one without
taking into account the fact that some of those things contain
others of those things.

Looking into "re.sub" might help, although in this case you
could solve the problem by doing one of several other things.

The simplest one that comes to mind is to sort the list of
keywords in reverse order by length of string, so that you
replace the longest items first.

-Peter
Jul 18 '05 #2
an***********@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

Jul 18 '05 #3
I can't claim to have studied your problem in detail, but I get
reasonable results from the following:

filename = 'Errors.txt'
S = open(filename,'r').read().split()
f = lambda x: (x[0]=='@' and x[6:] + '.0') or (x=='/' and x + '\n') or
x
open(filename,'w').write(' '.join(map(f,S)))

HTH

-------------------------------------------------------------------------
an***********@agip.it wrote in message news:<ma***************************************@py thon.org>...
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/

Jul 18 '05 #4
Sean McIlroy wrote:
f = lambda x: (x[0]=='@' and x[6:] + '.0') or (x=='/' and x + '\n') or
x


See "Inappropriate use of Lambda" in
http://www.python.org/moin/DubiousPython.

You're creating a named function, so there's no reason to use the
anonymous function syntax. Try:

def f(x):
return (x[0]=='@' and x[6:] + '.0') or (x=='/' and x + '\n') or x

or if it must be on one line:

def f(x): return (x[0]=='@' and x[6:] + '.0') or (x=='/' and x + '\n') or x

Personally, I would probably write this as IMHO more readable:

infile, outfile = open("Errors.txt"), open("Errors_2.txt")
for i, line in enumerate(infile):
permx, atpermx, rest = line.split(None, 2)
outfile.write(' '.join([permx, str(parameter_values[i]), rest]))
In action:

py> s = """\
.... '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
.... """
py> parameter_values = range(1, 17)
py> for i, line in enumerate(s.splitlines()):
.... permx, atpermx, rest = line.split(None, 2)
.... print ' '.join([permx, str(parameter_values[i]), rest])
....
'PERMX' 1 1 34 1 20 1 6
'PERMX' 2 1 34 21 41 1 6
'PERMX' 3 1 34 1 20 7 14
'PERMX' 4 1 34 21 41 7 14
'PERMX' 5 1 34 1 20 15 26
'PERMX' 6 1 34 21 41 15 26
'PERMX' 7 1 34 1 20 27 28
'PERMX' 8 1 34 21 41 27 28
'PERMX' 9 1 34 1 20 29 34
'PERMX' 10 1 34 21 41 29 34
'PERMX' 11 1 34 1 20 35 42
'PERMX' 12 1 34 21 41 35 42
'PERMX' 13 1 34 1 20 43 53
'PERMX' 14 1 34 21 41 43 53
'PERMX' 15 1 34 1 20 54 61
'PERMX' 16 1 34 21 41 54 61

STeVe
Jul 18 '05 #5
Alright, now it's too much. It's not enough that you're eliminating it
from the language, you have to stigmatize the lambda as well. You
should take some time to reflect that not everybody thinks the same
way. Those of us who are mathematically inclined like the lambda
because it fits in well with the way we already think. And besides, it
amounts to an explicit declaration that the function in question has
no side effects. And besides, it adds flexibility to the language. Go
ahead and throw it away, but you're making python less accessible for
those of us whose central concern is something other than programming.
("Single line" indeed!)
Jul 18 '05 #6
Sean McIlroy wrote:
Alright, now it's too much. It's not enough that you're eliminating it
from the language, you have to stigmatize the lambda as well.
You misunderstand me. I don't have a problem with lambda when it's
appropriate, e.g. when used as an expression, where a statement is
forbidden. See my post examining some of the uses in the standard
library[1]. But when you're defining a named function, you should use
the named function construct. That's what it's there for. =)
And besides, it amounts to an explicit declaration that the function
in question has no side effects.
Not at all. Some examples of lambdas with side effects:

py> a
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'a' is not defined
py> (lambda s: globals().__setitem__(s, 'side-effect'))('a')
py> a
'side-effect'

py> x = (lambda s: sys.stdout.write(s) or s.split())('side effect')
side effect
py> x
['side', 'effect']

py> s = set()
py> x = (lambda x: s.add(x) or x**2)(3)
py> s
set([3])
py> x
9

It is certainly possible to use lambda in such a way that it produces no
side effects, but it's definitely not guaranteed by the language.
And besides, it adds flexibility to the language.


I'm not sure I'd agree with flexibility... True, in some cases it can
allow more concise code, and occasionally it can read more clearly than
the other available options, but since you can use a function created
with def anywhere you can use a function created with lambda, I wouldn't
say that lambdas make Python any more flexible.

STeVe

[1]http://mail.python.org/pipermail/python-list/2004-December/257990.html
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas | last post by:
It looks like the String.replace doesn't work in IE6.1. Anyone else has the same problem. I am using newest service package of IE and Win2K. Thanks
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
14
by: Josh Baltzell | last post by:
I am having a lot more trouble with this than I thought I would. Here is what I want to do in pseudocode. Open c:\some.pdf Replace "Replace this" with "Replaced!" Save c:\some_edited.pdf I...
5
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
3
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.