在Ruby中,我該如何解析CSV文件並輸出相應的信息?例如:解析特定IP地址的CSV文件
require 'csv'
class CountryFilter
class << self
def find_specs_by_ip_address(ip)
CSV.foreach('GeoIPCountry.csv') do |row|
if row =~ Regexp.union(ip)
data = row.split(',')
return data[5]
else
return 'Unable to find country specifications'
end
end
end
end
end
puts CountryFilter.find_specs_by_ip_address('72.247.167.255')
CSV文件:
...
"72.247.88.0","72.247.89.255","1224169472","1224169983","US","United States"
"72.247.90.0","72.247.103.255","1224169984","1224173567","NL","Netherlands"
"72.247.104.0","72.247.144.255","1224173568","1224184063","US","United States"
"72.247.145.0","72.247.145.255","1224184064","1224184319","NL","Netherlands"
"72.247.146.0","72.247.167.255","1224184320","1224189951","US","United States"
"72.247.168.0","72.247.179.255","1224189952","1224193023","NL","Netherlands"
"72.247.180.0","72.247.181.255","1224193024","1224193535","US","United States"
"72.247.182.0","72.247.182.255","1224193536","1224193791","NL","Netherlands"
"72.247.183.0","72.247.183.255","1224193792","1224194047","US","United States"
...
如何可以解析爲IP地址,並輸出其來源國這個CSV文件?
我想前兩列實際上表示IP範圍。 – Felix