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