Skip to content

Instantly share code, notes, and snippets.

@fshp
Forked from maliqq/sberbank.rb
Last active January 4, 2017 20:11
Show Gist options
  • Save fshp/cdd60dece74621143626c9d633982c8b to your computer and use it in GitHub Desktop.
Save fshp/cdd60dece74621143626c9d633982c8b to your computer and use it in GitHub Desktop.
текстовая выписка сбербанка -> csv
# encoding: utf-8
def parse_date(s)
map = {'ЯНВ' => 1, 'ФЕВ' => 2, 'МАР' => 3, 'АПР' => 4, 'МАЙ' => 5, 'ИЮН' => 6, 'ИЮЛ' => 7, 'АВГ' => 8, 'СЕН' => 9, 'ОКТ' => 10, 'НОЯ' => 11, 'ДЕК' => 12}
s =~ /^(\d+)(.+?)(\d+)$/
Date.new($3.to_i + 2000, map[$2], $1.to_i)
end
def parse_date2(s, s2)
map = {'ЯНВ' => 1, 'ФЕВ' => 2, 'МАР' => 3, 'АПР' => 4, 'МАЙ' => 5, 'ИЮН' => 6, 'ИЮЛ' => 7, 'АВГ' => 8, 'СЕН' => 9, 'ОКТ' => 10, 'НОЯ' => 11, 'ДЕК' => 12}
s =~ /^(\d+)(.+?)$/
d = $1
m = $2
s2 =~ /^(\d+)(.+?)(\d+)$/
y = $3
Date.new(y.to_i + 2000, map[m], d.to_i)
end
require 'csv'
str = CSV.generate(:col_sep => ";") do |csv|
while line = gets
if line =~ /\s+(\d{2}.{3}) (\d{2}.{3}\d{2}) (\d+) (.+?)\s+(RUR|USD|EUR)?\s+([\d\.]*)\s+([\d\.]+)(CR)?\s+$/
d1 = parse_date2($1, $2)
d2 = parse_date($2)
if d1 > d2
d1 = Date.new(d1.year - 1, d1.mon, d1.mday)
end
csv << [d1, d2, $4, $5, $8 == 'CR' ? $7.to_f : - $7.to_f]
end
end
end
print str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment