diff --git a/interactive_brokers.py b/interactive_brokers.py index 10971f9..c8d1166 100644 --- a/interactive_brokers.py +++ b/interactive_brokers.py @@ -71,54 +71,54 @@ def isFileForBroker(cls, filename): @classmethod def parseFileToTxnList(cls, filename, tax_year): - f = open(filename) - # First 2 lines are headers. - f.readline() - f.readline() - txns = csv.reader(f, delimiter=',', quotechar='"') + with open(filename) as f: + # First 2 lines are headers. + f.readline() + f.readline() + txns = csv.reader(f, delimiter=',', quotechar='"') - txn_list = [] - part = None - box = None - entry_code = None + txn_list = [] + part = None + box = None + entry_code = None - for row in txns: - if row[0] == 'Part' and len(row) == 3: - box = None - if row[1] == 'I': - part = 1 - elif row[1] == 'II': - part = 2 - else: - utils.Warning('unknown part line: "%s"\n' % row) - elif row[0] == 'Box' and len(row) == 3: - if row[1] == 'A' or row[1] == 'B' or row[1] == 'C': - box = row[1] - entry_code = cls.DetermineEntryCode(part, box) - else: - utils.Warning('unknown box line: "%s"\n' % row) - elif row[0] == 'Data' and len(row) == 9: - if not entry_code: - utils.Warning( - 'ignoring data: "%s" as the code is not defined\n') - continue - txn = utils.Transaction() - txn.desc = row[1] - txn.buyDateStr = row[3] - txn.sellDateStr = row[4] - year = cls.TryParseYear(txn.sellDateStr) - txn.saleProceeds = cls.ParseDollarValue(row[5]) - txn.costBasis = cls.ParseDollarValue(row[6]) - if row[7]: - txn.adjustment = cls.ParseDollarValue(row[7]) - txn.entryCode = entry_code - if tax_year and year and year != tax_year: - utils.Warning('ignoring txn: "%s" as the sale is not from %d\n' % - (txn.desc, tax_year)) - else: - txn_list.append(txn) - txn = None - elif (row[0] != 'Header' and row[0] != 'Footer') or len(row) != 9: - utils.Warning('unknown line: "%s"\n' % row) + for row in txns: + if row[0] == 'Part' and len(row) == 3: + box = None + if row[1] == 'I': + part = 1 + elif row[1] == 'II': + part = 2 + else: + utils.Warning('unknown part line: "%s"\n' % row) + elif row[0] == 'Box' and len(row) == 3: + if row[1] == 'A' or row[1] == 'B' or row[1] == 'C': + box = row[1] + entry_code = cls.DetermineEntryCode(part, box) + else: + utils.Warning('unknown box line: "%s"\n' % row) + elif row[0] == 'Data' and len(row) == 9: + if not entry_code: + utils.Warning( + 'ignoring data: "%s" as the code is not defined\n') + continue + txn = utils.Transaction() + txn.desc = row[1] + txn.buyDateStr = row[3] + txn.sellDateStr = row[4] + year = cls.TryParseYear(txn.sellDateStr) + txn.saleProceeds = cls.ParseDollarValue(row[5]) + txn.costBasis = cls.ParseDollarValue(row[6]) + if row[7]: + txn.adjustment = cls.ParseDollarValue(row[7]) + txn.entryCode = entry_code + if tax_year and year and year != tax_year: + utils.Warning('ignoring txn: "%s" as the sale is not from %d\n' % + (txn.desc, tax_year)) + else: + txn_list.append(txn) + txn = None + elif (row[0] != 'Header' and row[0] != 'Footer') or len(row) != 9: + utils.Warning('unknown line: "%s"\n' % row) - return txn_list + return txn_list