Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A simple question string.replace

Reply
Thread Tools

A simple question string.replace

 
 
Haibao Tang
Guest
Posts: n/a
 
      01-31-2006
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.

 
Reply With Quote
 
 
 
 
Emile van Sebille
Guest
Posts: n/a
 
      01-31-2006

"Haibao Tang" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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



 
Reply With Quote
 
 
 
 
I V
Guest
Posts: n/a
 
      01-31-2006
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()

 
Reply With Quote
 
bruno at modulix
Guest
Posts: n/a
 
      01-31-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 '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments