-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.py
77 lines (57 loc) · 1.75 KB
/
process.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import argparse
import os
import sys
def generate_output_name(input_name):
fname, ext = os.path.splitext(input_name)
return '%s.output%s' % (fname, ext)
def process_line(line):
parts = line.strip().split(';')
try:
amount = float(parts[-1].replace(',','.'))
except ValueError:
return None
date = parts[1].replace('.','/')
credit_amount = 0
debit_amount = 0
if amount < 0:
debit_amount = -amount
else:
credit_amount = amount
description = ' '.join(p for p in parts[3:6]).strip()
return '%s;%s;%s;%f;%f\n' % (date, parts[0], description, credit_amount, debit_amount)
def process(input_name, output_name):
assert os.path.isfile(input_name)
with open(input_name) as f:
in_lines = f.readlines()
out_lines = []
for l in in_lines:
out_line = process_line(l)
if not out_line:
continue
out_lines.append(out_line)
with open(output_name,'w') as f:
print("Writing %s" % output_name)
f.writelines(out_lines)
def parse_args():
parser = argparse.ArgumentParser(description='Convert an Amazon Credit Card CSV file for importing into GNUCash')
parser.add_argument('input_name', help='The name of the input CSV file.')
parser.add_argument('-o', required=False, default=None, help='The name of the output filename')
parser.add_argument('-f', action='store_true', help='Overwrite the output file if it exists')
return parser.parse_args()
def main():
args = parse_args()
input_name = args.input_name
if args.o:
output_name = args.o
else:
output_name = generate_output_name(input_name)
if os.path.isfile(output_name):
print "The output file %s already exists" % output_name
if not args.f:
print "exiting"
sys.exit(1)
else:
print "Overwriting due to -f flag!"
process(input_name, output_name)
if __name__ == "__main__":
main()