我有一个从.CSV文件生成的数组,该文件充满了地址,我需要让它只提取街道名称,然后是Rd,Road,St等,然后 RubyGem Indirizzo可以完美地做到这一点,只是它无法识别以密苏里路或华盛顿大道等州命名的街道。有人知道如何解决这个问题吗?
#!ruby.exe
require 'Indirizzo'
require 'csv'
file = "Reports.csv"
begin
File.open(file, 'r')
rescue
print "Failed to open #{file}\n"
exit
end
data_file = File.new(file)
data = [] #initializes array for addresses from .csv
counter=0 #set counter up to allow for different sized files to be used without
issue
CSV.foreach(data_file, headers: true) do |row|
data << row.to_hash
counter+=1
end #goes through .csv one line at a time
data.reject!(&:empty?)
i=0
streets = []
column = "Street Name"
while (i<counter)
address = data[i][column]
street_name = address.gsub(/^((\d[a-zA-Z])|[^a-zA-Z])*/, '')
new_address = Indirizzo::Address.new(street_name, :expand_streets => false)
streetName = new_address.street
puts streetName
i+=1
end
答案 0 :(得分:2)
如果Indirizzo无法识别诸如Washington
之类的街道地址,则可能是一个错误,应在Github上进行报告。话虽如此,看来该宝石自2013年以来就没有更新过,因此我不会屏住呼吸进行修复。
同时,宝石StreetAddress
似乎能够解析这样的情况。自述文件中的示例的地址为1600 Pennsylvania Ave, Washington, DC, 20500
。此外,该宝石看起来更易于维护。它最近一次更新是在2017年。