Created
February 28, 2024 13:32
-
-
Save hosamaly/c8b2b40c0a5551d5060baedaea30645d to your computer and use it in GitHub Desktop.
Simplest QIF Parser
This file contains 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
# require 'date' | |
require 'pathname' | |
# returns an array of transactions, assuming the whole file relates to one account | |
def parse_qif_file(f) | |
qif_text = Pathname.new(f).readlines.drop_while { _1.start_with? '!' }.join | |
parse_qif(qif_text) | |
end | |
def parse_qif(text) | |
# see the file format in https://en.wikipedia.org/wiki/Quicken_Interchange_Format | |
transactions = text.split("^").map(&:strip).map { parse_qif_transaction(_1) } | |
end | |
def parse_qif_transaction(text) | |
text.lines.map(&:strip).reduce({}) do |transaction, line| | |
code = line[0] | |
value = line[1..] | |
key = | |
case code | |
when 'D' then :date | |
when 'P' then :payee | |
when 'T', 'U', '$' then :amount | |
end | |
transaction[key] = value | |
transaction | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment