DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

QIF To CSV Conversion Script In Ruby

04.20.2008
| 5731 views |
  • submit to reddit
        // Converts QIF files to CSV files

#!/usr/bin/env ruby

require 'rubygems'
require 'fileutils'

if ARGV.size < 1
        puts "Usage: #{$0} file.qif"
        exit
end

input = File.new(ARGV[0])
output = [File.basename(ARGV[0]).split('.')[0..-2], 'csv'].join('.')
output = File.new(output, 'w+')
output.write("date,amount,description,transaction id, address\n")

entries = input.read.split("^\n")
entries.compact
for entry in entries
        e = entry.match(/D(.*)\nT-?(.*)\nP(.*)\nN(.*)\nA(.*)\n/).to_a[1..-1]
        e[1] = e[1].to_f rescue nil
        e[-1] = "\"#{e[-1]}\""
        output.write("#{e.join(',')}\n")
end
    

Comments

Snippets Manager replied on Fri, 2008/09/12 - 8:56am

Seems to me that there's an error in the script. I don't know Ruby, but I just installed it on my box, and... ./qif-to-csv:20: undefined method `[]=' for nil:NilClass (NoMethodError) from ./qif-to-csv:18:in `each' from ./qif-to-csv:18