-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixofx.py
47 lines (40 loc) · 1.38 KB
/
fixofx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
import sys, getopt,re
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print __file__ + ' -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print __file__ + ' -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
strangeNameRe = re.compile('<NAME>(4835 \*{4} \*{4} 0133.*)$')
memoRe = re.compile('<MEMO>(.*)')
outfile = open(outputfile, 'w')
file = open(inputfile, 'r+')
for line in file:
match = strangeNameRe.match(line)
if match:
print 'found bad name'
nextline = file.next()
nextMatch = memoRe.match(nextline)
if nextMatch:
print 'found memo after'
name = match.group(1)
memo = nextMatch.group(1)
line = line.replace(name, memo)
nextline = nextline.replace(memo,name)
outfile.write(line)
outfile.write(nextline)
else:
outfile.write(line)
if __name__ == "__main__":
main(sys.argv[1:])