forked from M-tech-Creations/CSV-to-QIF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSV-to-QIF.py
138 lines (107 loc) · 3.94 KB
/
CSV-to-QIF.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'''**************************************************************************/
#
# @file CSV_to_QIF.py
# @author Mario Avenoso (M-tech Creations)
# @license MIT (see license.txt)
#
# Program to convert from a CSV to a QIF file using a definitions
# to describe how the CSV is formatted
#
# @section HISTORY
# v0.2 - Added payee ignore option 1/18/2016 feature update
# v0.1 - First release 1/1/2016 beta release
#
'''#**************************************************************************/
import os
import sys
import re
import csv
#
# @brief Takes given CSV and parses it to be exported to a QIF
#
# @params[in] inf_
# File to be read and converted to QIF
# @params[in] outf_
# File that the converted data will go
# @params[in] deff_
# File with the settings for converting CSV
#
#
def readCsv(inf_,outf_,deff_): #will need to receive input csv and def file
csvdeff = csv.reader(deff_, delimiter=',')
next(csvdeff,None)
for settings in csvdeff:
date_= int(settings[0]) #convert to int
amount_ = int(settings[2]) #How much was the transaction
memo_ = int(settings[3]) #discription of the transaction
payee_ = int(settings[4]) #Where the money is going
deli_ = settings[5] #How the csv is separated
header_ = int(settings[6]) #Set if there is a header to skip
csvIn = csv.reader(inf_, delimiter=deli_) #create csv object using the given separator
if header_ >= 1: #If there is a header skip the fist line
next(csvIn,None) #skip header
for row in csvIn:
writeFile(row[date_],row[amount_],row[memo_],row[payee_],outf_) #export each row as a qif entry
#
# @brief Receives data to be written to and its location
#
# @params[in] date_
# Data of transaction
# @params[in] amount_
# Amount of money for transaction
# @params[in] memo_
# Description of transaction
# @params[in] payee_
# Transaction paid to
# @params[in] filelocation_
# Location of the Output file
#
#
# https://en.wikipedia.org/wiki/Quicken_Interchange_Format
#
def writeFile(date_,amount_,memo_,payee_, filelocation_):
outFile = open(filelocation_,"a") #Open file to be appended
outFile.write("!Type:Cash\n") #Header of transaction, Currently all set to cash
outFile.write("D") #Date line starts with the capital D
outFile.write(date_)
outFile.write("\n")
outFile.write("T") #Transaction amount starts here
outFile.write(amount_)
outFile.write("\n")
outFile.write("M") #Memo Line
outFile.write(memo_)
outFile.write("\n")
if(payee_!=-1):
outFile.write("P") #Payee line
outFile.write(payee_)
outFile.write("\n")
outFile.write("^\n") #The last line of each transaction starts with a Caret to mark the end
outFile.close()
def convert():
error = 'Input error!____ Format [import.csv] [output.csv] [import.def] ____\n\n\
[import.csv] = File to be converted\n\
[output.qif] = File to be created\n\
[import.def] = Definition file describing csv file\n'
if (len(sys.argv) != 4): #Check to make sure all the parameters are there
print error
exit(1)
if os.path.isfile(sys.argv[1]):
fromfile = open(sys.argv[1],'r')
else:
print '\nInput error!____ import.csv: ' + sys.argv[1] + ' does not exist / cannot be opened !!\n'
exit(1)
try:
tofile = open(sys.argv[2],'a')
except:
print '\nInput error!____ output.csv: ' + sys.argv[2] + ' cannot be created !!\n'
exit(1)
if os.path.isfile(sys.argv[3]):
deffile = open(sys.argv[3],'r')
else:
print '\nInput error!____ import.def: ' + sys.argv[3] + ' does not exist / cannot be opened !!\n'
exit(1)
tofile = sys.argv[2]
readCsv(fromfile,tofile,deffile)
fromfile.close()
deffile.close()
convert()#Start