Parsing a tab delimited file with ruby csv?

I've been scouring the internet for hours looking for a good tutorial on
how to parse a tab delimited file with ruby's CSV library, the most I
know is that the library does in fact support tabs as the delimiter, I'd
really appricieate it if someone can post a link to a tutorial or a
quick code sample detailing how to tell csv to use tabs as a delimiter.
Thanks!

···

--
Posted via http://www.ruby-forum.com/.

Julio Capote wrote:

I've been scouring the internet for hours looking for a good tutorial on
how to parse a tab delimited file with ruby's CSV library, the most I
know is that the library does in fact support tabs as the delimiter, I'd
really appricieate it if someone can post a link to a tutorial or a
quick code sample detailing how to tell csv to use tabs as a delimiter.

For a case like this, there's really no point using the CSV library. If tabs
are used exclusively as delimiters and do not appear in the data, and if
each line is terminated with a linefeed, parsing such a file is very, very
easy with Ruby.

File.open("filename").each do |record|
   record.split("\t").each do |field|
      field.chomp!
      # do something here with each field
   end
end

Thanks!

Any time. :slight_smile:

···

--
Paul Lutus
http://www.arachnoid.com

I have never used it prior to trying this for you (so YMMV), but
(AFAICT) if you need to use the CSV library included with Ruby, the
third parameter in CSV#open is the delimiter, e.g.:

CSV.open('whatever.csv','r',"\t").each do |field|
puts field
end

Hope this helps,
Cory

···

On 9/8/06, Julio Capote <jcapote@gmail.com> wrote:

... I'd
really appricieate it if someone can post a link to a tutorial or a
quick code sample detailing how to tell csv to use tabs as a delimiter.

Take a look at the faster csv library

···

On 9/8/06, Paul Lutus <nospam@nosite.zzz> wrote:

Julio Capote wrote:

> I've been scouring the internet for hours looking for a good tutorial on
> how to parse a tab delimited file with ruby's CSV library, the most I
> know is that the library does in fact support tabs as the delimiter, I'd
> really appricieate it if someone can post a link to a tutorial or a
> quick code sample detailing how to tell csv to use tabs as a delimiter.

For a case like this, there's really no point using the CSV library. If
tabs
are used exclusively as delimiters and do not appear in the data, and if
each line is terminated with a linefeed, parsing such a file is very, very
easy with Ruby.

File.open("filename").each do |record|
   record.split("\t").each do |field|
      field.chomp!
      # do something here with each field
   end
end

> Thanks!

Any time. :slight_smile:

--
Paul Lutus
http://www.arachnoid.com

--
Mark Van Holstyn
mvette13@gmail.com
http://lotswholetime.com

Cory Chamblin wrote:

···

On 9/8/06, Julio Capote <jcapote@gmail.com> wrote:

... I'd
really appricieate it if someone can post a link to a tutorial or a
quick code sample detailing how to tell csv to use tabs as a delimiter.

I have never used it prior to trying this for you (so YMMV), but
(AFAICT) if you need to use the CSV library included with Ruby, the
third parameter in CSV#open is the delimiter, e.g.:

CSV.open('whatever.csv','r',"\t").each do |field|
puts field
end

Hope this helps,
Cory

I was also wondering if I could throw CSV.open an object instead of a
file, for instance, I get my csv file from another site using open-uri:
open(url) do |file|
  @body = file.read
end

--
Posted via http://www.ruby-forum.com/\.

I was also wondering if I could throw CSV.open an object instead of a
file, for instance, I get my csv file from another site using open-uri:
open(url) do |file|
        @body = file.read
end

Just make your own CSV::IOReader instead of having CSV#open do it for you:

CSV::IOReader.new(open(url),"\t").each do |thing|
  puts thing
end

HTH,
Cory

Cory Chamblin wrote:

I was also wondering if I could throw CSV.open an object instead of a
file, for instance, I get my csv file from another site using open-uri:
open(url) do |file|
        @body = file.read
end

Just make your own CSV::IOReader instead of having CSV#open do it for
you:

CSV::IOReader.new(open(url),"\t").each do |thing|
  puts thing
end

HTH,
Cory

Wow, cool! I didnt know you could do that. This is by far the most
helpful forum ive attended in a while. I have a tab delimited file that
has a bunch of fields separated by tabs in each line, I figure I would
need a multi-dimensional array to store the respective fields of every
line, no?

···

--
Posted via http://www.ruby-forum.com/\.

Julio Capote wrote:

Wow, cool! I didnt know you could do that. This is by far the most
helpful forum ive attended in a while. I have a tab delimited file that
has a bunch of fields separated by tabs in each line, I figure I would
need a multi-dimensional array to store the respective fields of every
line, no?

--

If you can manage with reading one line of fields in at a time, do
something with that line, and then discard it, you don't need a
multi-dimensional array.

You only need such an array if you want to do computation that involves
more than one line at a time.

···

~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasudev Ram
Software consulting and training

10.times say "Truly rural"
~~~~~~~~~~~~~~~~~~~~~~~~~~