Pulling a value from one CSV based on a value in another

前端 未结 1 1731
礼貌的吻别
礼貌的吻别 2021-01-27 16:23

I am trying to figure out the best way to pull a value from a CSV file called lookup.csv based on a value in master.csv, and then save the new file as

相关标签:
1条回答
  • 2021-01-27 16:38

    I'm using 1.9, where FasterCSV is available as CSV in the standard lib. First I'd create a lookup hash out of lookup.csv:

    cities = Hash[CSV.read('lookup.csv', :col_sep => ' | ').to_a[1..-1]]
    

    If the file is very big, you might want to iterate over it with CSV.foreach and build the hash row by row:

    cities = {}
    CSV.foreach('lookup.csv', :col_sep => ' | ', :headers => true, :return_headers => false) do |line|
      cities[line['City']] = line['City ID']  
    end  
    

    Then iterate over master.csv, do a lookup of the city in the hash and write that to output.csv:

    CSV.open('output.csv', "w", :headers => ['First Name', 'Last Name', 'City ID'], :write_headers => true) do |output|
      CSV.foreach('master.csv', :col_sep => ' | ', :headers => true, :return_headers => false) do |line|
        output << [line['First Name'], line['Last Name'], cities[line['City']]]
      end  
    end
    
    0 讨论(0)
提交回复
热议问题