Re: A simple question string.replace



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 'onurb@xxxxxxxxxxx'.split('@')])"
.



Relevant Pages

  • Re: A simple question string.replace
    ... >I have a two-column data file like this ... > Is it possible to substitue all '1.1' to some value else without using ... Emile ... Prev by Date: ...
    (comp.lang.python)
  • Re: A simple question string.replace
    ... Haibao Tang wrote: ... > Is it possible to substitue all '1.1' to some value else without using ... import sys ... Prev by Date: ...
    (comp.lang.python)