Last active
October 22, 2024 15:43
-
-
Save scimetfoo/4467a48cd4bc8e39436ead66845c1153 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(require '[clojure.string :as str] | |
'[clojure.java.io :as io]) | |
(when (not= (count *command-line-args*) 2) | |
(println "Usage: bb deutsche_bank_csv_to_qif.clj <input-csv> <output-qif>") | |
(println "Example: bb deutsche_bank_csv_to_qif.clj deutsche_bank.csv deutsche_bank.qif") | |
(System/exit 1)) | |
(def input-file (first *command-line-args*)) | |
(def output-file (second *command-line-args*)) | |
(def month-map | |
{"Jan" "01", "Feb" "02", "Mar" "03", "Apr" "04", "May" "05", "Jun" "06", | |
"Jul" "07", "Aug" "08", "Sep" "09", "Oct" "10", "Nov" "11", "Dec" "12"}) | |
(defn parse-date [date-str] | |
(if-let [[_ month day year] (re-matches #"(\w{3})\s+(\d{1,2}),\s+(\d{4})" date-str)] | |
(format "%02d/%s/%s" (Integer/parseInt day) (month-map month) year) | |
(throw (ex-info "Invalid date format" {:date date-str})))) | |
(defn csv-to-qif [line] | |
(let [[date counterparty description _ _ _ _ amount] (str/split line #";")] | |
(str "D" (parse-date date) "\n" | |
"T" amount "\n" | |
"P" counterparty "\n" | |
"M" description "\n" | |
"^\n"))) | |
(defn process-csv [] | |
(with-open [reader (io/reader input-file) | |
writer (io/writer output-file)] | |
(.write writer "!Type:Bank\n") | |
(doseq [line (rest (line-seq reader))] | |
(.write writer (csv-to-qif line))))) | |
(process-csv) | |
(println "Conversion complete. Output written to" output-file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment