A simple question string.replace

bruno at modulix onurb at xiludom.gro
Tue Jan 31 05:07:55 EST 2006


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 at xiludom.gro'.split('@')])"



More information about the Python-list mailing list