comparison replace_delimiter/replace_delimiter.py @ 0:295395b4b28f draft default tip

Uploaded
author saket-choudhary
date Tue, 07 Oct 2014 19:43:01 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:295395b4b28f
1 #!/usr/bin/env python
2 #By, Guruprasad Ananda.
3
4 import sys, re
5
6 def stop_err(msg):
7 sys.stderr.write(msg)
8 sys.exit()
9
10 def main():
11 if len(sys.argv) != 5:
12 stop_err("usage: convert_characters infile from_char to_char outfile")
13
14 try:
15 fin = open(sys.argv[1],'r')
16 except:
17 stop_err("Input file cannot be opened for reading.")
18
19 from_char = sys.argv[2]
20 to_char = sys.argv[3]
21
22 try:
23 fout = open(sys.argv[4],'w')
24 except:
25 stop_err("Output file cannot be opened for writing.")
26
27 char_dict = {
28 'T': '\t',
29 's': '\s',
30 'Dt': '\.',
31 'C': ',',
32 'D': '-',
33 'U': '_',
34 'P': '\|',
35 'Co': ':',
36 'Sc': ';'
37 }
38 from_ch = char_dict[from_char] + '+' #making an RE to match 1 or more occurences.
39 to_char = char_dict[to_char]
40 skipped = 0
41
42 for line in fin:
43 line = line.strip()
44 try:
45 fout.write("%s\n" %(re.sub(from_ch, to_char, line)))
46 except:
47 skipped += 1
48
49 fout.close()
50 fin.close()
51 if skipped:
52 print "Skipped %d lines as invalid." %skipped
53
54 if __name__ == "__main__":
55 main()