解析特定IP地址的CSV文件

时间:2016-12-29 19:27:18

标签: ruby csv parsing

在Ruby中,我如何解析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"
...

如何解析此CSV文件中的IP地址并输出其来源的国家/地区?

2 个答案:

答案 0 :(得分:1)

注释

  • row已经是包含多个单元格的CSV行,您无需拆分任何内容并定义data
  • 您的方法在两种情况下都在第一行之后返回:是否找到IP。
  • 当没有找到IP时返回nil可能比返回String更好。

重构代码

require 'csv'
class CountryFilter
  class << self
    def find_specs_by_ip_address(ip)
      CSV.foreach('GeoIPCountry.csv') do |row|
        return row.last if row.first(2).include?(ip)
      end
    end
  end
end

CountryFilter.find_specs_by_ip_address('72.247.167.255')
#=> "United States
CountryFilter.find_specs_by_ip_address('1.2.3.4')
#=> nil

V2

感谢@Felix的深刻见解。

此方法现在将ip1和ip2视为IP范围的边界,并检查ip是否在此范围内:

require 'csv'
require 'ipaddr'

module IPFilter
  def self.find_country_by_ip_address(ip)
    ip = IPAddr.new(ip).to_i
    CSV.foreach('GeoIPCountry.csv') do |_, _, ip1, ip2, _, country_name|
      return country_name if (ip1.to_i..ip2.to_i).cover?(ip)
    end
  end
end

p IPFilter.find_country_by_ip_address('72.247.167.123')
#=> "United States"
p IPFilter.find_country_by_ip_address('1.2.3.4')
#=> nil

Range#cover?

  

如果obj位于范围的开始和结束之间,则返回true。

答案 1 :(得分:0)

试试这个,

...
CSV.foreach('GeoIPCountry.csv') do |a, b, c, d, e, country|
  return country if a == ip || b == ip
end
...

这是如何运作的?

  • row已被逗号分割
  • 您可以在块参数中解压缩行以避免幻数

注意,最好给a, b, c, d, e个更有意义的名字。