Created
October 29, 2020 21:15
-
-
Save miguelrios/474abbf74b1abe9f7e580cf34c545662 to your computer and use it in GitHub Desktop.
Cleaning addresses through ruby_postal.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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