Re: A simple question string.replace
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: A simple question string.replace

From: bruno at modulix <onurb@xiludom.gro>
Date: Tue Jan 31 2006 - 11:07:55 CET

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@xiludom.gro'.split('@')])"
Received on Tue Feb 7 20:19:35 2006