Slide 41
Slide 41 text
def to_date(date)
months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP",
"OCT", "NOV", "DEC"]
tmp = date.scan(/([0-9]{2})([A-Z]{3})([0-9]{4})/).flatten
tmp[0] = tmp[0].to_i
tmp[1] = months.index(tmp[1]) + 1
tmp[2] = tmp[2].to_i
Date.new(tmp[2], tmp[1], tmp[0])
end
def to_float(var)
(var.blank? || var == '.' ? nil : var.to_s.tr(' ,', '_.').to_f)
end
def transcode_file(filepath)
input = File.open(filepath)
File.open(Rails.root.join("tmp", "import_utf8.csv"), "wb") do |f|
until input.eof?
content = input.read(2**16)
detection = CharlockHolmes::EncodingDetector.detect(content)
content = CharlockHolmes::Converter.convert(content,
detection[:encoding], "UTF-8")
f.write(content)
end
end
end
1h: some ‘utils’