Skip to content

Instantly share code, notes, and snippets.

@matallo
Created July 6, 2015 00:33
Show Gist options
  • Select an option

  • Save matallo/c17270be3dee3ba2c456 to your computer and use it in GitHub Desktop.

Select an option

Save matallo/c17270be3dee3ba2c456 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'csv'
f_all = File.open("all.xml")
xml_all = Nokogiri::XML(f_all)
xml_all.search('food').each do |food|
f_id = food.at("f_id").text
puts "-- Opening #{f_id}.xml"
f_food = File.open("#{f_id}.xml")
xml_food = Nokogiri::XML(f_food)
open("#{f_id}.csv", "w") do |csv|
component_list = []
csv << "f_id, f_ori_name, f_eng_name"
xml_food.search('foodvalue').each do |fv|
c_id = fv.search("c_id").text
component_list.push(c_id)
csv << ", \"c_#{c_id}\""
end
csv << "\n"
f_id = xml_food.at("f_id").text
f_ori_name = xml_food.at("f_ori_name").text
f_eng_name = xml_food.at("f_eng_name").text
csv << "\"#{f_id}\", \"#{f_ori_name}\", \"#{f_eng_name}\""
component_list.each do |component|
csv << ", \""
xml_food.search('foodvalue').each do |fv|
if fv.at("c_id").text == component
csv << fv.at("best_location").text
end
end
csv << "\""
end
csv << "\n"
end
puts "-- Saving #{f_id}.csv"
f_food.close
end
f_all.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment