May-24-2021, 12:14 AM
The problem is that code is written for Python 2.
In Python 3 takes csv module the input in text mode(
Then if i rewrite will be like this,tested code with a sample.csv ok.
In Python 3 takes csv module the input in text mode(
w
), whereas in Python 2 it took it in binary mode(wb
).Then if i rewrite will be like this,tested code with a sample.csv ok.
################################### # Name script: knab.py # # Parameter : Input file # # Author : Bastiaan Krijgsman # # Version : 0.1 # # Date : 03-05-2017 # ################################### from datetime import datetime import sys import csv import os class Bank: ifname = "" ofname = "" def __init__(self, ifile): self.ifname = ifile self.ofname = str(os.path.splitext(ifile)[0]) + "_" + str(datetime.now().year) + ".csv" def gencsvk(self): # indentation was wrong on this line ifile = open(self.ifname, "r", encoding='utf-8') reader = csv.reader(ifile, delimiter=',') ofile = open(self.ofname, "w", encoding='utf-8') print(ofile) writer = csv.writer(ofile, delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerow(["Rekening", "Datum", "Omschrijving", "Toelichting", "Opname", "Storting"]) # "Rekeningnummer","Transactiedatum","Valutacode","CreditDebet","Bedrag","Tegenrekeningnummer","Tegenrekeninghouder","Valutadatum; # Betaalwijze;Omschrijving;Type betaling;Machtigingsnummer;Incassant ID;Adres; next(reader) # indentation was wrong on this line next(reader) # indentation was wrong on this line for row in reader: print(row) rekening = row[0] datum = row[1] # omschrijving = row[10] + " " + row[11] + " " + row[12] + " " + row[13] + \ # " " + row[14] + " " + row[15] + " " + row[16] + " " + row[17] + \ # " " + row[18] omschrijving = row[9] # indentation was wrong on this line opname = 0 storting = 0 toelichting = row[5] + " " + row[6] + " " + row[8] + " " + row[13] if row[3] == "D": opname = row[4] if row[3] == "C": storting = row[4] nrow = rekening, datum, omschrijving, toelichting, opname, storting print(row[4] + " " + row[6]) writer.writerow(nrow) if len(sys.argv) < 2: print("No input file name given!") exit() bank = Bank(sys.argv[1]) bank.gencsvk()