Hey you can use faster csv instead using the standard csv ruby library.
Anyways the answer to your question is as below
CSV::Reader.parse(File.open('/root/Desktop/lukcy.csv', 'rb')) do |row|
p.row
end
This reads the entire row form the csv file.When you need a specific row to
be rread you can use the foreach iterator or something like row[0] which
returns the entire first column elements.
kranthi.
···
On Tue, Jul 22, 2008 at 12:43 AM, Li Chen <chen_li3@yahoo.com> wrote:
Hi all,
I need to read a CSV file with csv library. And I tried the example from
the library.
require 'csv'
CSV::Reader.parse(File.open('test.csv')) do |row|
puts row
# break if !row[0].is_null && row[0].data == 'stop'
end
Here is what I got:
#<CSV::Cell:0x2b7c350>
...
#<CSV::Cell:0x2b78b4c>
I wonder why I can't print out the string/row itself instead of the
address of the string/row.
Hey you can use faster csv instead using the standard csv ruby library.
Anyways the answer to your question is as below
CSV::Reader.parse(File.open('/root/Desktop/lukcy.csv', 'rb')) do |row|
p.row
end
This reads the entire row form the csv file.When you need a specific row
to
be rread you can use the foreach iterator or something like row[0] which
returns the entire first column elements.
kranthi.
Hi Kranthi,
I just need to print out the first column of each row and here are what
I get(only show two, first column of row 1 and row 2):
#<CSV::Cell:0x28284b0 @is_null=false, @data="A001:The new student was
visibly abashed when the teacher scolded him in front of the class for
reaching late.">
but what I actually need is
Frontside
A001:The new student was visibly abashed when the teacher scolded him in
front of the class for reaching late
I am not sure why I can't print out the part I need.
Hi,
To use faster csv you need to install the gem called fastercsv.
Then you can do the following
data = FasterCSV.read("/root/Desktop/<name of the csv file>" )
From this data you can access the columns the way you wish.
data[0][0] gives you the first column of the first row likewise you can get
the other rows also.
If you want to get the data for the entire column you can loop through and
get the details.
If you have any queries feel free to ask me .
kranthi.
···
On Tue, Jul 22, 2008 at 6:30 PM, Li Chen <chen_li3@yahoo.com> wrote:
kranthi reddy wrote:
> Hey you can use faster csv instead using the standard csv ruby library.
> Anyways the answer to your question is as below
> CSV::Reader.parse(File.open('/root/Desktop/lukcy.csv', 'rb')) do |row|
> p.row
> end
> This reads the entire row form the csv file.When you need a specific row
> to
> be rread you can use the foreach iterator or something like row[0] which
> returns the entire first column elements.
> kranthi.
Hi Kranthi,
I just need to print out the first column of each row and here are what
I get(only show two, first column of row 1 and row 2):
#<CSV::Cell:0x28284b0 @is_null=false, @data="A001:The new student was
visibly abashed when the teacher scolded him in front of the class for
reaching late.">
but what I actually need is
Frontside
A001:The new student was visibly abashed when the teacher scolded him in
front of the class for reaching late
I am not sure why I can't print out the part I need.
Hi,
To use faster csv you need to install the gem called fastercsv.
Then you can do the following
data = FasterCSV.read("/root/Desktop/<name of the csv file>" )
From this data you can access the columns the way you wish.
data[0][0] gives you the first column of the first row likewise you can
get
the other rows also.
If you want to get the data for the entire column you can loop through
and
get the details.
If you have any queries feel free to ask me .
kranthi.
Hi Kranthi,
Thank you very much for the help. I solve my problem by using fastercsv
instead of csv and it works perfect.
Hi,
To use faster csv you need to install the gem called fastercsv.
Then you can do the following
data = FasterCSV.read("/root/Desktop/<name of the csv file>" )
From this data you can access the columns the way you wish.
data[0][0] gives you the first column of the first row likewise you can
get
the other rows also.
If you want to get the data for the entire column you can loop through
and
get the details.
If you have any queries feel free to ask me .
kranthi.
Hi,
Sorry if this sounds like a dumb question but I'm new to all of this.
How do I find and install fastercsv? (I'm using a Mac.)
When I run "ruby -cw fasterCSV.rb" I get syntax ok.
However, when I run the script I get an error that states "uninitialized
constant FasterCSV (NameError)"
Is there anything special I need to do to initialize the class?
Frank Guerino wrote:
Is there anything special I need to do to initialize the class?
You only required the rubygems library wich is necassary to load Gems
(the packages you can install via RubyGems). So, you need to require
fastercsv, too.
Btw. the documentation for fastercsv should have been installed along
with the Gem, it should reside in the
lib/ruby/gems/<RUBY_VERSION>/doc/fastercsv subdirectory of your Ruby
installation. But if you're satisfied with the command-line docs, type
$ ri FasterCSV
I just realized that in Ruby 1.9 there shouldn't be the need to install
fastercsv separately, because:
irb(main):001:0> require "fastercsv"
Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus
support for Ruby 1.9's m17n encoding engine.
Frank Guerino wrote:
Is there anything special I need to do to initialize the class?
You only required the rubygems library wich is necassary to load Gems
(the packages you can install via RubyGems). So, you need to require
fastercsv, too.
Btw. the documentation for fastercsv should have been installed along
with the Gem, it should reside in the
lib/ruby/gems/<RUBY_VERSION>/doc/fastercsv subdirectory of your Ruby
installation. But if you're satisfied with the command-line docs, type
$ ri FasterCSV
Have fun!
Marvin
Hi,
I'm still working with my existing 1.8.6 interpreter and getting errors
after installing the gem FasterCSV. Here's the code...
./fastercsv.rb:53: uninitialized constant FasterCSV (NameError)
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in
`require'
from fasterCSV.rb:50
frank-guerinos-macbook-air:CSV guerino1$
I checked the documentation using "ri FasterCSV" but there doesn't seem
to be anything obvious.
I just realized that in Ruby 1.9 there shouldn't be the need to install
fastercsv separately, because:
irb(main):001:0> require "fastercsv"
Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus
support for Ruby 1.9's m17n encoding engine.
Marvin
Hi Marvin,
Thanks again. Being new to all this, how do upgrade to Ruby 1.9.x?
Everything else seems to be easy (like installing gems) so I'm assuming
there's a command to upgrade Ruby, too, correct?
./fastercsv.rb:53: uninitialized constant FasterCSV (NameError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
from fasterCSV.rb:50
frank-guerinos-macbook-air:CSV guerino1$
I checked the documentation using "ri FasterCSV" but there doesn't seem to be anything obvious.
Frank,
If your test program is named fastercsv.rb (regardless of case), rename it to something else, like mytest.rb or something.
···
--
RMagick is looking for a maintainer. Email me if you're interested.
Thanks again. Being new to all this, how do upgrade to Ruby 1.9.x?
Everything else seems to be easy (like installing gems) so I'm assuming
there's a command to upgrade Ruby, too, correct?
Thanks,
Frank
Hi Frank,
No, it's not that easy...
As I said, I've no experience with Macs, but you should be able to
compile Ruby yourself in any case. Download the recent version from Download Ruby (that should be 1.9.1-p243),
unpackage the tarball and run this commands in it:
$ ./configure
$ make
# make install
You may should think about passing some options to the configure step,
especially --prefix and --program_suffix (type ./configure --help for
help on those). That would allow you to have two Rubies installed at the
same time.
Maybe someone with Mac experience can suggest another way?