Skip to content

Instantly share code, notes, and snippets.

@kp666
Created July 20, 2018 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kp666/895a41a112694a36af97e66b6f8f1a24 to your computer and use it in GitHub Desktop.
Save kp666/895a41a112694a36af97e66b6f8f1a24 to your computer and use it in GitHub Desktop.
class Country < ActiveRecord::Base
end
file='news.txt'
def find_country_code(line)
line[/\(\S*\)/,0].to_s.gsub(/[()]/, "")
end
def find_country(country_code)
Country.where(country_code: country_code).last
end
CSV.open("reworked.csv", "w") do |f|
File.readlines(file).each do |line|
country_code = find_country_code(line)
country = find_country(country_code).country rescue "unknown"
f << [country]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment