A simple question string.replace

H

Haibao Tang

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.
 
E

Emile van Sebille

Haibao Tang said:
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
 
I

I V

Haibao said:
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()
 
B

bruno at modulix

Haibao said:
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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top