Parsing a tab delimited file with ruby csv?

J

Julio Capote

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!
 
C

Cory Chamblin

... 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
 
J

Julio Capote

Cory said:
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
 
C

Cory Chamblin

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
 
J

Julio Capote

Cory said:
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?
 
V

vasudevram

Julio said:
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
http://www.dancingbison.com
10.times say "Truly rural"
~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top