2

In a file, I have the following lines

NetConn_msa[0].time=0.0

NetConn_msa[1].time=0.0 etc for 60 elements. 

I need to write a script to change the time from 0.0 to 0.5.

I started with st.replace("delay=0.0","delay=0.05") and then tried to find the string to replace

sub=re.compile("\[\d+\]\.\w+=[0]\.[0]")
result=sub.search(st)

Can someone please help me since I am very new to programming. Thanks!

2
  • 3
    You have not really asked a question. Commented May 2, 2011 at 20:51
  • What are you trying to achieve, in the first place? Commented May 2, 2011 at 21:13

4 Answers 4

2

Why not do this the really easy way: manually edit the file so that the offending 60 lines are replaced by:

for x in range(60):
    NetConn_msa[x].time = 0.5
Sign up to request clarification or add additional context in comments.

Comments

1

Script:

import re

x = """NetConn_msa[0].time=0.0
NetConn_msa[59].time=0.0
NetConn_msa[0].time=9.9
NetConn_msa[60].time=0.0
NetConn_msa[61].time=0.0
NetConn_msa[99].time=0.0
NetConn_msa[100].time=0.0
NetConn_msa[0].other=0.0
NetConn_msa[0].time=0.01
PseudoNetConn_msa[59].time=0.0
Foo[0].bar=0.0"""

print "=== not strict enough ==="
print re.sub("(\]\.\w+)=0.0","\\1=0.5",x)

print "=== stricter ==="
print re.sub(r"(NetConn_msa\[[1-5]?\d\]\.time)=0.0\b", r"\1=0.5", x)

Output:

=== not strict enough ===
NetConn_msa[0].time=0.5
NetConn_msa[59].time=0.5
NetConn_msa[0].time=9.9
NetConn_msa[60].time=0.5
NetConn_msa[61].time=0.5
NetConn_msa[99].time=0.5
NetConn_msa[100].time=0.5
NetConn_msa[0].other=0.5
NetConn_msa[0].time=0.51
PseudoNetConn_msa[59].time=0.5
Foo[0].bar=0.5
=== stricter ===
NetConn_msa[0].time=0.5
NetConn_msa[59].time=0.5
NetConn_msa[0].time=9.9
NetConn_msa[60].time=0.0
NetConn_msa[61].time=0.0
NetConn_msa[99].time=0.0
NetConn_msa[100].time=0.0
NetConn_msa[0].other=0.0
NetConn_msa[0].time=0.01
PseudoNetConn_msa[59].time=0.0
Foo[0].bar=0.0

5 Comments

I think your "stricter" entry is the same as "no strict enough"
@Aif: If you mean that my longer version is still not strict enough, please give an example; otherwise, please explain what you mean.
Sorry it was unclear: look the output. Stricter also matched Foo[O].bar=0.0.
@Aif: You are mistaken. Look at the output. "Stricter" did NOT change the 0.0 to 0.5.
Sorry, I didn't figure out the "sub" stuff. (was late.. :p)
0
x = """NetConn_msa[0].time=0.0

NetConn_msa[1].time=0.0 etc for 60 elements. """

print re.sub("(\]\.\w+)=0.0","\\1=0.5",x)

#NetConn_msa[0].time=0.5

#NetConn_msa[1].time=0.5 etc for 60 elements. 

Comments

0

Why not do this the easy way?

st.replace('.time=0.0\n','.time=0.5\n')

1 Comment

Because you could change text that should not be changed, and not change text that should be changed. Examples of the latter: '.time=0.0 \n' and '.time=0.0 # comment\n'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.