Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1077881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:38:32+00:00 2026-05-16T21:38:32+00:00

I would like to parse a CSV file so that each row is treated

  • 0

I would like to parse a CSV file so that each row is treated like an object with the header-row being the names of the attributes in the object. I could write this, but I’m sure its already out there.

Here is my CSV input:

"foo","bar","baz"
1,2,3
"blah",7,"blam"
4,5,6

The code would look something like this:

CSV.open('my_file.csv','r') do |csv_obj|
  puts csv_obj.foo   #prints 1 the 1st time, "blah" 2nd time, etc
  puts csv.bar       #prints 2 the first time, 7 the 2nd time, etc
end

With Ruby’s CSV module I believe I can only access the fields by index. I think the above code would be a bit more readable. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:38:32+00:00Added an answer on May 16, 2026 at 9:38 pm

    Using Ruby 1.9 and above, you can get a an indexable object:

    CSV.foreach('my_file.csv', :headers => true) do |row|
      puts row['foo'] # prints 1 the 1st time, "blah" 2nd time, etc
      puts row['bar'] # prints 2 the first time, 7 the 2nd time, etc
    end
    

    It’s not dot syntax but it is much nicer to work with than numeric indexes.

    As an aside, for Ruby 1.8.x FasterCSV is what you need to use the above syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
I would like to parse the folder name and file name from passed argument.
I would like to parse through and display and picture and description for each
I use JSON.NET and I would like to parse the following object which I
I have a text file and I would like to parse it using regular
I have an HTML file and i would like to parse through it using
Currently I am using MySQLi to parse a CSV file into a Database, that
I have a csv file that looks like this: Please note, there are no
I'm using ruby 1.9.2 I'm trying to parse a CSV file that contains some
I have a singleton object in iOS that when instantiated parses a CSV file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.