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)
#5
Running the commando python knab.py import.csv gave the error.

Traceback (most recent call last):
File "C:\TMP\knab.py", line 63, in <module>
bank.gencsvk()
File "C:\TMP\knab.py", line 30, in gencsvk
writer.writerow(["Rekening", "Datum", "Omschrijving",
TypeError: a bytes-like object is required, not 'str'



I use Python 3.9.5 on Windows 10, also the the csv has two more columns (Referentie;Boekdatum;)


###################################
# 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, "rb")
        reader = csv.reader(ifile, delimiter=';')
 
        ofile = open(self.ofname, "wb")
        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;
        reader.next()  # indentation was wrong on this line
        reader.next()  # indentation was wrong on this line
 
        for row in reader:
 
            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()
Reply


Messages In This Thread
RE: How do I use tabs and spaces? (how to resolve error TabError: inconsistent) - by Onanism - May-20-2021, 04:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with removing spaces and tabs from a string msqpython 14 4,251 Jan-21-2021, 10:48 PM
Last Post: deanhystad
  How Can I Resolve This Error JayJayOi 10 7,569 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