5 if __name__ == '__main__':
6 parser = argparse.ArgumentParser(description='Import')
7 parser.add_argument('-o', '--output', required=True, help='output file')
8 parser.add_argument('input_fnames', nargs='+', help='input files to merge')
9 args = parser.parse_args()
14 for f in input_files.values():
17 for fname in args.input_fnames:
19 input_files[fname] = open(fname, 'rtU')
20 except (IOError, OSError):
24 # Read and process the first 3 lines
25 counters = {'spam': {}, 'nonspam': {}}
26 for fname, infile in input_files.items():
27 version_line = infile.readline()
29 counter_line = infile.readline().strip()
30 v, counter, tag = counter_line.split('\t')
34 'Bad v-tag in file %s line %s: unknown v-tag %s, '
35 'expected "v", got %r' % (fname, counter_line, v))
38 counters['spam'][fname] = int(counter)
42 elif tag == 'num_nonspam':
44 counters['nonspam'][fname] = int(counter)
51 'Bad tag in file %s line %s: unknown tag %s, '
52 'expected "num_spam" or "num_nonspam", got %r' % (
53 fname, counter_line, tag))
55 counters_total = {'spam': 0, 'nonspam': 0}
56 for key, files in counters.items():
57 counters_total[key] = sum(files.values())
60 with open(args.output, 'wt') as outfile:
61 outfile.write(version_line)
62 for key, value in counters_total.items():
63 outfile.write('v\t%d\tnum_%s\n' % (value, key))
64 for infile in input_files.values():
67 except (IOError, OSError):