Python Forum
How do I use tabs and spaces? (how to resolve error TabError: inconsistent)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I use tabs and spaces? (how to resolve error TabError: inconsistent)
#11
Try delimiter=b',' , quotechar=b'"' perhaps.
Reply
#12
The problem is that code is written for Python 2.
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()
Onanism likes this post
Reply
#13
When i run the script it gives empty rows
Reply
#14
Onanism Wrote:When i run the script it gives empty rows
Can you post example csv file for which it gives empty rows ?
Reply
#15
 
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')
        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()
KNAB EXPORT;;;;;;;;;;;;;;;;
Rekeningnummer;Transactiedatum;Valutacode;CreditDebet;Bedrag;Tegenrekeningnummer;Tegenrekeninghouder;Valutadatum;Betaalwijze;Omschrijving;Type betaling;Machtigingsnummer;Incassant ID;Adres;Referentie;Boekdatum;
"NL00KNAB0000000000";"06-05-2021";"EUR";"D";"115,89";"NL47RABSDFE64";"AF74 B.V.";"06-05-2021";"iDEAL";"Siet et al AF74 B.V.";"";"";"";"";"C1E0634ZJ";"06-05-2021";
"NL00KNAB0000000000";"06-05-2021";"EUR";"C";"150";"5775SDF39";"Kees de Leeuw";"06-05-2021";"Ontvangen betaling";"";"";"";"";"";"C1E06423VPZK";"06-05-2021";
"NL00KNAB0000000000";"03-05-2021";"EUR";"D";"1,85";"BE85739SDF55506";"Alipay";"03-05-2021";"iDEAL";"Hk E-Commerce";"";"";"";"";"C1E023405B";"03-05-2021";
"NL00KNAB0000000000";"20-04-2021";"EUR";"D";"3,39";"BE85739SD55506";"FS Services S.A.";"21-04-2021";"iDEAL";"Aly EUROPE LTD S.A";"";"";"";"";"C1D20werXHI";"20-04-2021";
Reply
#16
It worked for me with Python 3.8, here is the output file (input file was ona.csv, output file is ona_2022.csv)
Output:
"Rekening","Datum","Omschrijving","Toelichting","Opname","Storting" "NL00KNAB0000000000","06-05-2021","Siet et al AF74 B.V.","NL47RABSDFE64 AF74 B.V. iDEAL ","115,89","0" "NL00KNAB0000000000","06-05-2021","","5775SDF39 Kees de Leeuw Ontvangen betaling ","0","150" "NL00KNAB0000000000","03-05-2021","Hk E-Commerce","BE85739SDF55506 Alipay iDEAL ","1,85","0" "NL00KNAB0000000000","20-04-2021","Aly EUROPE LTD S.A","BE85739SD55506 FS Services S.A. iDEAL ","3,39","0"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with removing spaces and tabs from a string msqpython 14 3,916 Jan-21-2021, 10:48 PM
Last Post: deanhystad
  How Can I Resolve This Error JayJayOi 10 7,311 Jan-18-2018, 02:59 PM
Last Post: JayJayOi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020