any better way to do it?

Benyang Tang btang at pacific.jpl.nasa.gov
Thu Mar 9 02:57:27 EST 2000


Here I am doing some replacement in a file. It may just take one line in sed. I am wondering whether there is a shorter way in python to do it. Thanks for any suggestion.

#=======================
nStart='123'
nStop='223'

file1 = open('data','r')
lines = file1.readlines()
file1.close()

p1 = re.compile(r'^\s*nStateStart\s*=\s*(\d+)')
p2 = re.compile(r'^\s*nStateStop\s*=\s*(\d+)')

linesOut = []
for line in lines:
    line = p1.sub(nStart,line)
    line = p2.sub(nStop,line)
    linesOut.append(line)

file1 = open('data','w')
file1.writelines(linesOut)
file1.close()
#======================



More information about the Python-list mailing list