My csv file has the semicolon as delimiter. I can open it with
r = csv.reader(infile, delimiter=";")
without any issues. The problem is that I want to open the file as a dict. The csv.DictReader
class doesn't have the delimiter
option.
My code:
import os
import csv
fields = ["Buchungstag", "Buchungstext", "Beguenstigter/Zahlungspflichtiger", "", "", "Betrag", "Verwendungszweck"]
with open("bank.csv") as infile, open("temp2.csv", "w", newline="") as outfile:
r = csv.DictReader(infile)
w = csv.DictWriter(outfile, fields, extrasaction="ignore")
w.writeheader()
for row in r:
w.writerow(row)
I tried opening the file and only loading certain fields, which works if I modify the file beforehand, replacing the ;
with ,
(I used notepad++ for this) – but I would like to skip this part and have the file directly opened.