Storing CSV data in a Ruby hash

Say I have a CSV file with 4 fields,

ID,name,pay,age

and about 32,000 records.

What is the best way to insert this into a hash in Ruby?

In other words, an example entry would look like this:

{: rec1 => {: id => "00001" ,: name => "Bob" ,: pay => 150 ,: age => 95}}

Thanks for the help!

+3
source share
4 answers

You can use Excelsiorrubygem for this:

csv = ...
result = Hash.new
counter = 1
Excelsior::Reader.rows(csv) do |row|
   row_hash = result[("rec#{counter}".intern)] = Hash.new

   row.each do |col_name, col_val|
      row_hash[col_name.intern] = col_val
   end
   counter += 1
end

# do something with result...
+5
source

Usually we want to use the field :idfor the Hash key, because it will be the same as the primary key in the database table:

{"00001" => {:name => "Bob", :pay => 150, :age => 95 } }

This will create a hash similar to this:

require 'ap'

# Pretend this is CSV data...
csv = [
  %w[ id     name  pay age ],
  %w[ 1      bob   150 95  ],
  %w[ 2      fred  151 90  ],
  %w[ 3      sam   140 85  ],
  %w[ 31999  jane  150 95  ]

]

# pull headers from the first record
headers = csv.shift

# drop the first header, which is the ID. We'll use it as the key so we won't need a name for it.
headers.shift

# loop over the remaining records, adding them to a hash
data = csv.inject({}) { |h, row| h[row.shift.rjust(5, '0')] = Hash[headers.zip(row)]; h }
ap data

# >> {
# >>     "00001" => {
# >>         "name" => "bob",
# >>          "pay" => "150",
# >>          "age" => "95"
# >>     },
# >>     "00002" => {
# >>         "name" => "fred",
# >>          "pay" => "151",
# >>          "age" => "90"
# >>     },
# >>     "00003" => {
# >>         "name" => "sam",
# >>          "pay" => "140",
# >>          "age" => "85"
# >>     },
# >>     "31999" => {
# >>         "name" => "jane",
# >>          "pay" => "150",
# >>          "age" => "95"
# >>     }
# >> }
+3
source

Ruby Gem smarter_csv, CSV () CSV . chunking, CSV , Resque Mongoid MongoMapper.

It comes with many useful options - check out the documentation on GitHub

require 'smarter_csv'
filename = '/tmp/input.csv'
array = SmarterCSV.process(filename)

=>

[ {:id=> 1, :name => "Bob", :pay => 150, :age => 95 } ,
 ...
]

See also:

+1
source
Hash[*CSV.read(filename, :headers => true).flat_map.with_index{|r,i| ["rec#{i+1}", r.to_hash]}]
0
source

Source: https://habr.com/ru/post/1779815/


All Articles