[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Parsing a tab delimited file with ruby csv?

Julio Capote

9/9/2006 2:31:00 AM

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

6 Answers

Paul Lutus

9/9/2006 2:42:00 AM

0

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. :)

--
Paul Lutus
http://www.ara...

Cory Chamblin

9/9/2006 3:52:00 AM

0

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

Julio Capote

9/9/2006 3:58:00 AM

0

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

Cory Chamblin

9/9/2006 4:55:00 AM

0

> 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

Julio Capote

9/9/2006 6:14:00 AM

0

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

vasudevram

9/9/2006 4:27:00 PM

0


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