A simple question string.replace

Discussion in 'Python' started by Haibao Tang, Jan 31, 2006.

  1. Haibao Tang

    Haibao Tang Guest

    I have a two-column data file like this
    1.1 2.3
    2.2 11.1
    4.3 1.1
    ....
    Is it possible to substitue all '1.1' to some value else without using
    re.

    Thanks.
     
    Haibao Tang, Jan 31, 2006
    #1
    1. Advertising

  2. "Haibao Tang" <> wrote in message
    news:...
    >I have a two-column data file like this
    > 1.1 2.3
    > 2.2 11.1
    > 4.3 1.1
    > ...
    > Is it possible to substitue all '1.1' to some value else without using
    > re.
    >


    Yes --

    data = """1.1 2.3
    2.2 11.1
    4.3 1.1"""

    newdata = data.replace("1.1","1.x")

    but that's probably not what you want.

    Emile
     
    Emile van Sebille, Jan 31, 2006
    #2
    1. Advertising

  3. Haibao Tang

    I V Guest

    Haibao Tang wrote:
    > Is it possible to substitue all '1.1' to some value else without using
    > re.


    You could try:

    import sys

    values = sys.stdin.readline().split()
    while values:
    results = []
    for value in values:
    if value != '1.1':
    results.append(value)
    else:
    results.append('something else')

    print '\t'.join(results)
    values = sys.stdin.readline().split()
     
    I V, Jan 31, 2006
    #3
  4. Haibao Tang wrote:
    > I have a two-column data file like this
    > 1.1 2.3
    > 2.2 11.1
    > 4.3 1.1
    > ...
    > Is it possible to substitue all '1.1' to some value else without using
    > re.


    I suppose that you don't want '11.1' to be affected.

    raw_data="""
    1.1 2.3
    2.2 11.1
    4.3 1.1
    """

    data = filter(None, [line.strip().split() \
    for line in raw_data.split('\n')])
    processed = [[item == '1.1' and 'X.X' or item for item in pair] \
    for pair in data]
    result = "\n".join(["\t".join(pair) for pair in processed])

    Not sure this is the fastest solution...

    BTW, may I ask why you don't want to use regexp ?


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno at modulix, Jan 31, 2006
    #4
    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. Kun
    Replies:
    4
    Views:
    314
    Dennis Lee Bieber
    Mar 26, 2006
  2. Alun
    Replies:
    3
    Views:
    4,523
    Masudur
    Feb 18, 2008
  3. Prasad S
    Replies:
    2
    Views:
    236
    Dr John Stockton
    Aug 27, 2004
  4. SM
    Replies:
    4
    Views:
    211
  5. V S Rawat
    Replies:
    5
    Views:
    309
    Richard Cornford
    Jul 3, 2007
Loading...

Share This Page