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

  • Home
  • SEARCH
  • 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 6751791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:58:07+00:00 2026-05-26T12:58:07+00:00

Using Ruby 1.9.2 I need to parse a CSV file, and output lines with

  • 0

Using Ruby 1.9.2 I need to parse a CSV file, and output lines with each header key and individual value paired together, with line numbers.

header: key1, key2, key3
row1: a, b, c
row2: d, , f

expected output:

1
key1 a
key2 b
key3 c

2

key1 d
key2
key3 f

Right now working on just combining the header with the values, and I’m failing pretty hard.

require 'csv'

header = File.open('TEXT.CSV', &:readline) 
keys = header.split(",")


values = CSV.read("TEXT.CSV")

def combine(a,b)
  zipped = a.zip(b)
  Hash[zipped]
end

keyvalue = values.each do |i|
  combine(keys,i)
end

Any ideas what I did wrong in there?

  • 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-26T12:58:08+00:00Added an answer on May 26, 2026 at 12:58 pm
    require 'csv'
    lineN = 0
    
    CSV.read( filename  ).each do |arr|
      if lineN == 0
        headers = arr
      else
        puts "line #{lineN}"
    
        headers.zip(arr).each do |a|
          puts "#{a.first} : #{a.last}"
        end
      end
      lineN += 1
    end
    

    creates:

    line 1
    key1 : a
    key2 : b
    key3 : c
    
    line 2
    key1 : d
    key2 : 
    key3 : f
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using ruby to read a file and I need to somehow parse some
I need to print escaped characters to a binary file using Ruby. The main
I need to parse a xml document in ruby, using rexml, that has the
I am using Ruby on Rails and I need to store a search result
Using Your Ruby Gem(s) You will need to add /home/username/ruby/gems to the include path.
Helo, I am pretty new to Ruby (using 1.8.6) and need to know whether
I've been using Textmate for Ruby/Python scripting for awhile and now have a need
Using Ruby (newb) and Regex, I'm trying to parse the street number from the
I need to parse C#, Ruby and Python source code to generate some reports.
I'm using Ruby on Rails and need to run a block of Ruby code

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.