Jump to content

Parsing fixed-width fields in Ruby/Rails

simonstl's Photo
Posted Nov 06 2009 11:54 AM
6796 Views

I need to import a large table of data that arrives in fixed-width text format, and a few related smaller pieces that also come in different fixed-width formats.

I know that I can write a bit of code that unpacks lines based on an array of lengths, but is there anything more flexible out there? The smaller pieces in particular seem to change occasionally, and I'd rather not lock format details into code.

Tags:
1 Subscribe


1 Reply

+ 1
  kyle.burton's Photo
Posted Nov 10 2009 10:00 AM

If the goal is just to externalize the configuration, there are several implementation strategies (yaml, arguments to a function, additional parameters if it's a rails controller etc).

This is an example of using command line paramters:

#!/usr/bin/env ruby
unless 3 == ARGV.size 
  puts "#$0 <field-widths> <input-file> <output-file>"
  exit -1
end

field_widths = ARGV[0].split(',').map &:to_i
input_file   = ARGV[1]
output_file  = ARGV[2]

field_pattern = "A#{field_widths.join('A')}"
outp = File.open(output_file,"w")
File.foreach filename do |line|
  row = line.unpack field_pattern
  outp.puts row.join "\t"
end
outp.close