read CSV file using csv library

L

Li Chen

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.


Thanks,

Li
 
K

kranthi reddy

[Note: parts of this message were removed to make it a legal post.]

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

Li Chen

kranthi said:
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:0x28297c0 @is_null=false, @data="Frontside">

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

Thanks,

Li
 
K

kranthi reddy

[Note: parts of this message were removed to make it a legal post.]

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.


kranthi said:
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:0x28297c0 @is_null=false, @data="Frontside">

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

Thanks,

Li
 
L

Li Chen

kranthi said:
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.

Li
 
F

Frank Guerino

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

Thanks for the help,

Frank
 
M

Marvin Gülker

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

Thanks for the help,

Frank

You can do this via the command-line tool RubyGems. Try:
# gem install fastercsv

I'm not familiar with Macs, but that should do it. To learn more about
RubyGems, visit: http://docs.rubygems.org/

Marvin

PS: And add the line
require "rubygems"
to your scripts if you're using Ruby 1.8.
 
F

Frank Guerino

Marvin said:
You can do this via the command-line tool RubyGems. Try:
# gem install fastercsv

I'm not familiar with Macs, but that should do it. To learn more about
RubyGems, visit: http://docs.rubygems.org/

Marvin

PS: And add the line
require "rubygems"
to your scripts if you're using Ruby 1.8.

Hi Marvin,

Thanks for the quick reply. I installed fastercsv using the command you
described and there were no issues, so it seems to have installed
propertly.

Next, I wrote the following code:

require "rubygems"
csvData = FasterCSV.read("./dataInput3.csv")
myString = csvData[0][0]
puts myString


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?

Thanks,

Frank
 
M

Marvin Gülker

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.

require "rubygems"
require "fastercsv"
csvData = FasterCSV.read("./dataInput3.csv")
myString = csvData[0][0]
puts myString


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
 
M

Marvin Gülker

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
 
F

Frank Guerino

Marvin said:
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?

Thanks,

Frank
 
F

Frank Guerino

Marvin said:
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.

require "rubygems"
require "fastercsv"
csvData = FasterCSV.read("./dataInput3.csv")
myString = csvData[0][0]
puts myString


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

---------------------------

require "rubygems"
require "fastercsv"

csvData = FasterCSV.read("./dataInput3.csv")
myString = csvData[0][0]
puts myString

---------------------------

When I run it, I get the following errors...

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

Any ideas?

Thanks,

Frank
 
T

Tim Hunter

Frank said:
When I run it, I get the following errors...

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

Frank Guerino

Tim said:
Frank,

If your test program is named fastercsv.rb (regardless of case), rename
it to something else, like mytest.rb or something.

Hi Tim,

Thank you! Lesson learned... Never name my example programs after
language constructs! ;-)

It works and I can now say I love FasterCSV!

My Best,

Frank Guerino
 
M

Marvin Gülker

Frank said:
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?

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
http://www.ruby-lang.org/en/downloads/ (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?

Marvin
 
P

Patrick Okui

Maybe someone with Mac experience can suggest another way?

1. Download and install macports from http://www.macports.org/ (follow =20=

the advice about changing your .profile and setting your $PATH)

2. Run "sudo port install ruby19 rubygems"

3. Install fastercsv into the new ruby19 directory "sudo gem install =20
fastercsv"

4. Wash, rinse and repeat step 3 for other gems you need.
 

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,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top