The following is a separate application for replacing the ID numbers in a data file.
[code=Python]def replaceID2(file List, idDict, *args):
pattkey = re.compile('|'. join([r'\b(%s)' % item for item in args]))
for i, line in enumerate(fileL ist):
m = pattkey.match(l ine)
if m:
try:
mnum = pattnum.search( line)
id = int(mnum.group( 0))
if id in idDict:
s = str(idDict[id])
fileList[i] = '%s%s%s' % (line[:mnum.start()], s, \
line[mnum.start()+le n(s):])
except:
print 'No valid ID number found.'
return fileList
if __name__ == '__main__':
fn = 'sample_data'
pointID = {1:86010101,2:8 6010102,3:86010 103,4:86010104, \
5:86010106,6:96 010104,7:860101 10,8:86010201,\
9:86010115,3280 504:96010403}
curveID = {1:10001101,2:1 0001102,3:10001 103,4:10001104, \
6:10001106}
fileList = open(fn).readli nes()
replaceID2(file List, curveID, *['Line', 'Rect', 'Tria'])
replaceID2(file List, pointID, *['Point',])
f = open('sample_da ta1', 'w')
f.write(''.join (fileList))
f.close()[/code]
[code=Python]def replaceID2(file List, idDict, *args):
pattkey = re.compile('|'. join([r'\b(%s)' % item for item in args]))
for i, line in enumerate(fileL ist):
m = pattkey.match(l ine)
if m:
try:
mnum = pattnum.search( line)
id = int(mnum.group( 0))
if id in idDict:
s = str(idDict[id])
fileList[i] = '%s%s%s' % (line[:mnum.start()], s, \
line[mnum.start()+le n(s):])
except:
print 'No valid ID number found.'
return fileList
if __name__ == '__main__':
fn = 'sample_data'
pointID = {1:86010101,2:8 6010102,3:86010 103,4:86010104, \
5:86010106,6:96 010104,7:860101 10,8:86010201,\
9:86010115,3280 504:96010403}
curveID = {1:10001101,2:1 0001102,3:10001 103,4:10001104, \
6:10001106}
fileList = open(fn).readli nes()
replaceID2(file List, curveID, *['Line', 'Rect', 'Tria'])
replaceID2(file List, pointID, *['Point',])
f = open('sample_da ta1', 'w')
f.write(''.join (fileList))
f.close()[/code]
Comment