473,396 Members | 1,786 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,396 software developers and data experts.

trouble with replace

I have a string (xdata) and theres a newline after every 17 characters
of the string. I was wondering how I can replace multiple substrings
multiple times within a string? To put it another way, this is what i
want to do.

Substring to find ("abcdef") replace it with ("highway")
search again, substring to find ("defgef") replace it with
("news").Search again and find ("effwer") replace it with
("monitor").Example string to search under is '''defgefabcdefyuuuu
effwerbyuuuterrfr'''

I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')
Basically I would want to get the output of "newshighwayyuuuu
monitorbyuuuterrfr"

Thanks, any help would be appreciated.

Aug 12 '06 #1
7 1808
In <11**********************@m79g2000cwm.googlegroups .com>, f pemberton
wrote:
I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')
`replace()` does not work in place. You have to bind the result to a name
like::

xdata = xdata.replace('abcdef', 'highway')

Ciao,
Marc 'BlackJack' Rintsch
Aug 12 '06 #2
Marc 'BlackJack' Rintsch wrote:
In <11**********************@m79g2000cwm.googlegroups .com>, f pemberton
wrote:
I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')

`replace()` does not work in place. You have to bind the result to a name
like::

xdata = xdata.replace('abcdef', 'highway')

Ciao,
Marc 'BlackJack' Rintsch
lol, you probably will not believe me but I actually knew that already.
I just forgot to add that part in my original post. When I try and
replace what happens is the first replace works fine but when I try and
do a second replace on a different part of the same string, the program
will print the string a second time(which I do not want). How can you
do 2 or more replaces in one line of code?

Aug 12 '06 #3
f pemberton wrote:
Marc 'BlackJack' Rintsch wrote:
In <11**********************@m79g2000cwm.googlegroups .com>, f pemberton
wrote:
I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')
`replace()` does not work in place. You have to bind the result to a name
like::

xdata = xdata.replace('abcdef', 'highway')

Ciao,
Marc 'BlackJack' Rintsch

lol, you probably will not believe me but I actually knew that already.
I just forgot to add that part in my original post. When I try and
replace what happens is the first replace works fine but when I try and
do a second replace on a different part of the same string, the program
will print the string a second time(which I do not want).
Um, don't print the string until after you're done replacing? :-)

Seriously though, since there are no print statements, etc.., in your
posted code, it's hard to understand exactly what your problem is.
How can you
do 2 or more replaces in one line of code?
In any event, there is a neat method of "single-pass multiple string
substitution using a dictionary" here:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/81330 It uses
a regular expression, so I'd guess it wouldn't be to hard to get it to
work in multiline mode.

Peace,
~Simon

Aug 12 '06 #4

Simon Forman wrote:
f pemberton wrote:
Marc 'BlackJack' Rintsch wrote:
In <11**********************@m79g2000cwm.googlegroups .com>, f pemberton
wrote:
>
I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')
>
`replace()` does not work in place. You have to bind the result to a name
like::
>
xdata = xdata.replace('abcdef', 'highway')
>
Ciao,
Marc 'BlackJack' Rintsch
lol, you probably will not believe me but I actually knew that already.
I just forgot to add that part in my original post. When I try and
replace what happens is the first replace works fine but when I try and
do a second replace on a different part of the same string, the program
will print the string a second time(which I do not want).

Um, don't print the string until after you're done replacing? :-)

Seriously though, since there are no print statements, etc.., in your
posted code, it's hard to understand exactly what your problem is.
How can you
do 2 or more replaces in one line of code?

In any event, there is a neat method of "single-pass multiple string
substitution using a dictionary" here:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/81330 It uses
a regular expression, so I'd guess it wouldn't be to hard to get it to
work in multiline mode.

Peace,
~Simon
Or just:

xdata = xdata.replace('abcdef', 'highway').replace('defgef',
'news').replace('effwer', 'monitor')

Hahahahaha

~Simon

Aug 12 '06 #5
>>pats = ['abcdef', 'defgef', 'effwer']
>>reps = ['highway', 'news', 'monitor']
s = 'defgefabcdefyuuuu\n\n\n effwerbyuuuterrfr'
reduce(lambda x,y: x.replace(*y), zip(pats,reps), s)
'newshighwayyuuuu\n\n\n monitorbyuuuterrfr'

f pemberton wrote:
I have a string (xdata) and theres a newline after every 17 characters
of the string. I was wondering how I can replace multiple substrings
multiple times within a string? To put it another way, this is what i
want to do.

Substring to find ("abcdef") replace it with ("highway")
search again, substring to find ("defgef") replace it with
("news").Search again and find ("effwer") replace it with
("monitor").Example string to search under is '''defgefabcdefyuuuu
effwerbyuuuterrfr'''

I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')
Basically I would want to get the output of "newshighwayyuuuu
monitorbyuuuterrfr"

Thanks, any help would be appreciated.
Aug 12 '06 #6
>>>pats = ['abcdef', 'defgef', 'effwer']
>>>reps = ['highway', 'news', 'monitor']
s = 'defgefabcdefyuuuu\n\n\n effwerbyuuuterrfr'
reduce(lambda x,y: x.replace(*y), zip(pats,reps), s)

The reduce() method fairly works well if you have it as a
dictionary as well:
>>m = {'effwer': 'monitor', 'abcdef': 'highway', 'defgef': 'news'}
s = 'defgefabcdefyuuuu\n\n\n effwerbyuuuterrfr'
reduce(lambda x,y: x.replace(y, m[y]), m.keys(), s)
'newshighwayyuuuu\n\n\n monitorbyuuuterrfr'

One does get somewhat unpredictable results if one of the
replacements contains a target search pattern (or creates it in
the resulting string):
>>s = 'onetwothree'
m = {'one':'two', 'two':'three', 'three':'one'}
reduce(lambda x,y: x.replace(y, m[y]), m.keys(),s)
'twothreetwo'
>>m['three'] = 'four'
m['four'] = 'two'
reduce(lambda x,y: x.replace(y, m[y]), m.keys(),s)
'twothreefour'

Just a few more ideas...

-tkc

Aug 12 '06 #7
This might well take the trouble out ot the OP's replace:

http://cheeseshop.python.org/pypi/SE/2.2%20beta

Regards

Frederic

----------------------------------------------------------------------------------
And here is how it works:
>>text = '''defgefabcdefyuuuu
effwerbyuuuterrfr'''
>>import SE
String_Editor = SE.SE ( 'abcdef=highway defgef=news effwer=monitor')
print String_Editor (text)
newshighwayyuuuu

monitorbyuuuterrfr
----------------------------------------------------------------------------------

----- Original Message -----
From: "f pemberton" <fp***********@yahoo.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Saturday, August 12, 2006 6:49 PM
Subject: trouble with replace

I have a string (xdata) and theres a newline after every 17 characters
of the string. I was wondering how I can replace multiple substrings
multiple times within a string? To put it another way, this is what i
want to do.

Substring to find ("abcdef") replace it with ("highway")
search again, substring to find ("defgef") replace it with
("news").Search again and find ("effwer") replace it with
("monitor").Example string to search under is '''defgefabcdefyuuuu
effwerbyuuuterrfr'''

I've tried using replace but its not working for me.
xdata.replace('abcdef', 'highway')
xdata.replace('defgef', 'news')
xdata.replace('effwer', 'monitor')
Basically I would want to get the output of "newshighwayyuuuu
monitorbyuuuterrfr"

Thanks, any help would be appreciated.

--
http://mail.python.org/mailman/listinfo/python-list
Aug 13 '06 #8

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

Similar topics

5
by: fingermark | last post by:
I'm using feedparser to parse the following: <div class="indent text">Adv: Termite Inspections! Jenny Moyer welcomes you to her HomeFinderResource.com TM A "MUST See &amp;hellip;</div> I'm...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
14
by: aroraamit81 | last post by:
Hi, I am facing a trouble. I have some Session variables in my code and somehow my session variables are getting mixed up with other users. For example User A has access to 10 companies and...
0
by: scottf35 | last post by:
Hi, I am working on (read that - upgrading) an application. This application creates an HTTPWebRequest object, populates it with values which are then sucked out of the Request.Form object (eg...
6
by: Just Me | last post by:
Any ideas on this. I am trying to loop through an xml document to remove attributes, but Im having so much trouble, any help is appreciated //THIS IS THE EXCEPTION ( SEE CODE LINE WHERE FAILURE...
2
by: John Nagle | last post by:
I'm trying to clean up a bad ASCII string, one read from a web page that is supposedly in the ASCII character set but has some characters above 127. And I get this: File...
5
by: Middletree | last post by:
My results after doing some stuff are going to have names separated by commas. Example: James & Beth Williams John & Mary Smith Ross & Rachel Gellar Willy & Wanda Wonka But the number of...
2
by: Zethex | last post by:
At the moment i'm doing a piece of work for school and I'm stuck at the moment. I have a list of words, for example: Sentence = I have another list which I need to use to replace certain...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.