Here is what the text file looks like:
#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 5 3 4 6 4 5 4 7 5 5 10
24 9 7 7 13 7 9 9 14 10 10 20
and this is what it does:
#Number of Bits
12
#Data
0 0 0 0 0 0 0 0 0 0 0 0
12 0 3 4 6 4 5 4 7 5 5 10
24 0 7 7 13 7 9 9 14 10 10 20
Here is what the Python 2.0 code looks like:
Expand|Select|Wrap|Line Numbers
- def nthzero(dataList, nth, n):
- '''
- Replace the nth element of each list in the data list with 'n'
- '''
- for item in dataList:
- item[nth] = n
- return dataList
- fn = 'outfile.txt'
- f = open(fn)
- s = f.next()
- prefix = s
- while s.strip() != '#Data':
- s = f.next()
- prefix += s
- lineList = [line.strip().split() for line in f]
- f.close()
- elem = 0
- repl = '0'
- lineList = nthzero(lineList, elem, repl)
- fn1 = 'outfile.txt'
- f = open(fn1, 'w')
- outList = []
- for line in lineList:
- outList.append(' '.join(line))
- f.write('%s%s' % (prefix, '\n'.join(outList)))
- f.close()