read CSV file using csv library

Discussion in 'Ruby' started by Li Chen, Jul 21, 2008.

  1. Li Chen

    Li Chen Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Jul 21, 2008
    #1
    1. Advertising

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

    On Tue, Jul 22, 2008 at 12:43 AM, Li Chen <> 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.
    >
    >
    > Thanks,
    >
    > Li
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    kranthi reddy, Jul 22, 2008
    #2
    1. Advertising

  3. Li Chen

    Li Chen Guest

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Jul 22, 2008
    #3
  4. [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.


    On Tue, Jul 22, 2008 at 6:30 PM, Li Chen <> 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: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
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    kranthi reddy, Jul 22, 2008
    #4
  5. Li Chen

    Li Chen Guest

    kranthi reddy wrote:
    > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Jul 22, 2008
    #5
  6. kranthi reddy wrote:
    > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Frank Guerino, Oct 7, 2009
    #6
  7. Frank Guerino wrote:
    > 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Marvin Gülker, Oct 7, 2009
    #7
  8. Marvin Gülker wrote:

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


    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Frank Guerino, Oct 7, 2009
    #8
  9. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Marvin Gülker, Oct 7, 2009
    #9
  10. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Marvin Gülker, Oct 7, 2009
    #10
  11. Marvin Gülker wrote:
    > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Frank Guerino, Oct 7, 2009
    #11
  12. Marvin Gülker wrote:
    > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Frank Guerino, Oct 8, 2009
    #12
  13. Li Chen

    Tim Hunter Guest

    Frank Guerino wrote:

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

    --
    RMagick is looking for a maintainer. Email me if you're interested.
     
    Tim Hunter, Oct 8, 2009
    #13
  14. Tim Hunter wrote:
    > Frank Guerino wrote:
    >
    >> 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.


    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Frank Guerino, Oct 8, 2009
    #14
  15. On Oct 7, 2009, at 10:00 PM, Frank Guerino wrote:

    > It works and I can now say I love FasterCSV!


    Always good to hear. ;)

    James Edward Gray II
     
    James Edward Gray II, Oct 8, 2009
    #15
  16. Frank Guerino wrote:
    >
    > 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Marvin Gülker, Oct 8, 2009
    #16
  17. Li Chen

    Patrick Okui Guest

    On Oct 8, 2009, at 6:25 PM, Marvin G=FClker wrote:

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

    --
    patrick=
     
    Patrick Okui, Oct 8, 2009
    #17
  18. Frank Guerino, Oct 12, 2009
    #18
  19. Li Chen

    Azmi Farih Guest

    Azmi Farih, Mar 23, 2010
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tintin92
    Replies:
    1
    Views:
    1,806
    Andrew Thompson
    Feb 14, 2007
  2. tom
    Replies:
    1
    Views:
    1,349
    ahaskins
    Jul 13, 2009
  3. jliu66
    Replies:
    0
    Views:
    559
    jliu66
    Oct 19, 2007
  4. Alex Dowad
    Replies:
    4
    Views:
    314
    Michel Demazure
    May 1, 2010
  5. Sacha Rook

    csv read clean up and write out to csv

    Sacha Rook, Nov 2, 2012, in forum: Python
    Replies:
    2
    Views:
    244
    Hans Mulder
    Nov 2, 2012
Loading...

Share This Page