trouble with replace

Discussion in 'Python' started by f pemberton, Aug 12, 2006.

  1. f pemberton

    f pemberton Guest

    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.
    f pemberton, Aug 12, 2006
    #1
    1. Advertising

  2. In <>, 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
    Marc 'BlackJack' Rintsch, Aug 12, 2006
    #2
    1. Advertising

  3. f pemberton

    f pemberton Guest

    Marc 'BlackJack' Rintsch wrote:
    > In <>, 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?
    f pemberton, Aug 12, 2006
    #3
  4. f pemberton

    Simon Forman Guest

    f pemberton wrote:
    > Marc 'BlackJack' Rintsch wrote:
    > > In <>, 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/Cookbook/Python/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
    Simon Forman, Aug 12, 2006
    #4
  5. f pemberton

    Simon Forman Guest

    Simon Forman wrote:
    > f pemberton wrote:
    > > Marc 'BlackJack' Rintsch wrote:
    > > > In <>, 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/Cookbook/Python/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
    Simon Forman, Aug 12, 2006
    #5
  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)

    '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.
    >
    Jason Nordwick, Aug 12, 2006
    #6
  7. f pemberton

    Tim Chase Guest

    >>>> 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
    Tim Chase, Aug 12, 2006
    #7
  8. This might well take the trouble out ot the OP's replace:

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

    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" <>
    Newsgroups: comp.lang.python
    To: <>
    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
    Anthra Norell, Aug 13, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    365
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    331
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,479
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    215
    Dr John Stockton
    Aug 27, 2004
  5. Replies:
    3
    Views:
    168
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page