Skip to content

Instantly share code, notes, and snippets.

@miguelrios
Created October 29, 2020 21:15
Show Gist options
  • Save miguelrios/474abbf74b1abe9f7e580cf34c545662 to your computer and use it in GitHub Desktop.
Save miguelrios/474abbf74b1abe9f7e580cf34c545662 to your computer and use it in GitHub Desktop.
Cleaning addresses through ruby_postal.
require 'ruby_postal/parser'
require 'csv'
c= CSV.read("users_simple.csv")
RECIPIENT = 0
ADDRESS = 1
CSV.open("addresses_clean.csv", "wb") do |csv|
c.each do |item|
parsed = Postal::Parser.parse_address(item[ADDRESS]).map do |x| [x[:label], x[:value]] end.to_h
splitted = item[ADDRESS].split(",").map do |i| i.strip.chomp end
cleaned = splitted.reject{|e| [parsed[:postcode],parsed[:city],parsed[:state]].include?(e.downcase)}.join(" ")
item[3] = cleaned
item[2] = parsed[:postcode]
csv << item
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment