Skip to content

Instantly share code, notes, and snippets.

@hosamaly
Created February 28, 2024 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hosamaly/c8b2b40c0a5551d5060baedaea30645d to your computer and use it in GitHub Desktop.
Save hosamaly/c8b2b40c0a5551d5060baedaea30645d to your computer and use it in GitHub Desktop.
Simplest QIF Parser
# 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