require 'json' content = File.open('country_codes_table.html').read #_lines[0..20].join # content = %q{ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # } codes = content.scan(/tt>(\w+)<\/tt/m) names = content.scan(/title="([\w|\s|\'|\-|\,]+)"/m) zipped = codes.zip(names) hash = {} zipped.each do |pair| pair = pair.flatten hash[pair.first.to_s.downcase] = pair.last end json = JSON.pretty_generate(hash) File.open('ISO-3166-2_codes.en.json', 'w+') do |f| f.puts json end
CodeCountry nameYearccTLDISO 3166-2Notes
Andorra1974.adISO 3166-2:AD
AEUnited Arab Emirates1974.aeISO 3166-2:AE
AFAfghanistan1974.afISO 3166-2:AF