Created
August 6, 2012 18:58
-
-
Save kgrz/3277554 to your computer and use it in GitHub Desktop.
Converts a Gzipped text file into a csv. Text file has a specific structure. And this is exactly how not to code in Ruby
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 'zlib' | |
file = *ARGV | |
file.each do |eachfile| | |
Zlib::GzipReader.open(eachfile) do |f| | |
account = "" | |
result = [] | |
f.each_line do |line| | |
next unless line[/\d{17}/] | |
account = line[6,11] | |
a = { | |
:date_one => line[17, 8], | |
:date_two => line[25, 8], | |
:narration => line[33, 150].strip, | |
:transaction_crdr => line[183], | |
:tr_amount => line[184, 16].to_i/100.00, | |
:total_amt => line[200], | |
:total_balance => line[201, 16].to_i/100.00 | |
} | |
b = a.collect {|k,v| "#{v}"} | |
result << b.join(',') | |
end | |
File.open("#{account}_output.csv", "w") do |f| | |
f << result.join("\n") | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment