[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parsing Fixed Width File

Nick da G

4/14/2009 10:13:00 PM

Hello, all.
Does anyone know of a good or any for that matter library (gem) to
parse fixed width files?

I've looked into active-warehouse - but it looks like it's for rails
only and docs are a bit underdeveloped for it.

Any ideas anyone?
--
Posted via http://www.ruby-....

4 Answers

Anthony Eden

4/14/2009 11:16:00 PM

0

Did you look at the ActiveWarehouse ETL docs:
http://activewarehouse.rubyforge.org/docs/activewarehous... ?
If nothing else you might be able to look at the existing fixed width
file parser and use that as a starting point.

Sincerely,
Anthony Eden

On Tue, Apr 14, 2009 at 6:13 PM, Nick da G <nick.gorbikoff@gmail.com> wrote=
:
> Hello, all.
> Does anyone know of a good =A0or any for that matter library (gem) to
> parse fixed width files?
>
> I've looked into active-warehouse - but it looks like it's for rails
> only and docs are a bit underdeveloped for it.
>
> Any ideas anyone?
> --
> Posted via http://www.ruby-....
>
>



--=20
GMU/IT d- s: a32 C++(++++)$ UL@ P--- L+(++) !E W+++$ !N o? K? w--- !O
M++ V PS+ PE Y PGP t+ !5 X- R tv b++ DI+ D++ G- e++ h---- r+++ y++++**

http://...

Andrew Timberlake

4/15/2009 3:08:00 AM

0

On Wed, Apr 15, 2009 at 12:13 AM, Nick da G <nick.gorbikoff@gmail.com> wrot=
e:
> Hello, all.
> Does anyone know of a good =A0or any for that matter library (gem) to
> parse fixed width files?
>
> I've looked into active-warehouse - but it looks like it's for rails
> only and docs are a bit underdeveloped for it.
>
> Any ideas anyone?
> --

It's pretty simple so I'm not sure that a library is needed

fields =3D [5,12,5,2,20] #<- the size of each field
field_pattern =3D "A#{fields.join('A')}"
File.foreach(filename) do |line|
row =3D line.unpack(field_pattern)
... #use row[0] etc for each field
end

Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Nick da G

4/15/2009 5:32:00 PM

0

Anthony Eden wrote:
> Did you look at the ActiveWarehouse ETL docs:
> http://activewarehouse.rubyforge.org/docs/activewarehous... ?
> If nothing else you might be able to look at the existing fixed width
> file parser and use that as a starting point.
>
> Sincerely,
> Anthony Eden
>


Yeah I looked into it - still requires use of ActiveRecord.
--
Posted via http://www.ruby-....

Nick da G

4/15/2009 5:35:00 PM

0

> It's pretty simple so I'm not sure that a library is needed
>
> fields = [5,12,5,2,20] #<- the size of each field
> field_pattern = "A#{fields.join('A')}"
> File.foreach(filename) do |line|
> row = line.unpack(field_pattern)
> ... #use row[0] etc for each field
> end
>
> Andrew Timberlake
> http://ramblingso...
> http://www.linkedin.com/in/andrew...
>
> "I have never let my schooling interfere with my education" - Mark Twain


Yeah that's pretty much what I ended up doing.
Created an object that defines each record (line)

then pass it at a line from file, and get an object back that I can do
what I want with - like outputing to csv .

I was jsut wondering if there was a more simple and elegant solution.
But I guess you can't go simpler than that. :-) Thanks for a response!
--
Posted via http://www.ruby-....