[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Read CSV-File (Comma Separated Values

hu8

1/30/2005 5:56:00 PM

Hi!

First of all, im a real beginner in ruby.

How can I read data from a file? How to separate it, is my second
question.
So, i don't want to become the File from the STDIN, but to set the uri
from the file with uri = '/home/hu8/Documents/data.csv' or somthing
like this...

btw: How can I read user input, so i make:
print "Type A or B"

and then the user must type a or b and i become in a variable (like
input = user.input()?)?
Or does exists a function like the PHP-function fgetcsv()?

6 Answers

Julius Plenz

1/30/2005 6:08:00 PM

0

* hu8 <jakobfastenbauer196@hotmail.com> [2005-01-30]:
^^^
You should post with a real name here.

> How can I read data from a file? How to separate it, is my
> second question.

begin
file = File.open('filename')
while not file.eof?
values = file.gets.split(/,/)
# do some stuff with it
end
ensure
file.close
end

With this code, you have an array "values" within the
while-loop. "values" contains all the fields which were
seperated by comma in the file.

Julius

James Britt

1/30/2005 7:08:00 PM

0

Julius Plenz wrote:
> * hu8 <jakobfastenbauer196@hotmail.com> [2005-01-30]:
> ^^^
> You should post with a real name here.
>
>
>>How can I read data from a file? How to separate it, is my
>>second question.
>
>
> begin
> file = File.open('filename')
> while not file.eof?
> values = file.gets.split(/,/)
> # do some stuff with it
> end
> ensure
> file.close
> end

You might do better to leave the hard work to ruby:

IO.readlines( 'filename' ).each{ |line|
ary_values = line.split(/,/)
# do some stuff with it
}

By using the block form you let Ruby handle the check for EOF and close
the file when done.

James


Stefan Lang

1/30/2005 7:56:00 PM

0

James Britt wrote:

> You might do better to leave the hard work to ruby:
>
> IO.readlines( 'filename' ).each{ |line|
> ary_values = line.split(,)
^^^
Shouldn't there be quotes around the comma?
line.split(',')

> # do some stuff with it
> }
>
> By using the block form you let Ruby handle the check for EOF and close
> the file when done.
>

James Britt

1/30/2005 11:54:00 PM

0

Stefan Lang wrote:
> James Britt wrote:
>
>
>>You might do better to leave the hard work to ruby:
>>
>>IO.readlines( 'filename' ).each{ |line|
>>ary_values = line.split(,)
>
> ^^^
> Shouldn't there be quotes around the comma?
> line.split(',')

Or regexp slashes, which is what I used, and which is what appears when
I view my post in my E-mail reader (Thunderbird).

line.split(/,/)

Odd.


James


Assaph Mehr

1/31/2005 3:47:00 AM

0

For CSV files take a look at the CSV module in the stdlib:
http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/...

For opening arbitrary URIs take a look at open-uri in the std-lib:
http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/...
HTH,
Assaph

Stefan Lang

1/31/2005 5:17:00 PM

0

James Britt wrote:

> Stefan Lang wrote:
>> James Britt wrote:
>>
>>
>>>You might do better to leave the hard work to ruby:
>>>
>>>IO.readlines( 'filename' ).each{ |line|
>>>ary_valuesÃ? =Ã? line.split(,)
>>
>> ^^^
>> Shouldn't there be quotes around the comma?
>> line.split(',')
>
> Or regexp slashes, which is what I used, and which is what appears when
> I view my post in my E-mail reader (Thunderbird).
>
> line.split(/,/)
>
> Odd.

Sorry, seems to be a bug in KNode. The slashes are actually in
the message but my KNode doesn't show them!

> James