如何解析基于CSV的IF条件

时间:2017-04-05 13:10:37

标签: ruby csv rubygems

我正在尝试解析我从Nexpose的API调用的CSV报告:

 nsc.list_reports.each do | report |
  puts report.name
  report_id = report.config_id
  report_summary = nsc.generate_report(report_id, true)

  report = nsc.download(report_summary.uri)
  puts report    


  csv = CSV.read(report, :headers => true, :converters => :all).select do|row|
    row['number available'] > 0 && row['Score'] >=9
  end

  csv.each do |row|
    puts row['name']
  end
end

但基本上这是它给我的格式

ID,name,title,Score,number available
1,"test1","title1",4,3
2,"test2","title2",8,0
3,"test3","title3",9,0
4,"test4","title4",10,6
5,"test5","title5",5,22
6,"test6","title6",9,1
7,"test7","title7",2,5

我的目标是只找到那些得分为"" > = 9和"可用数量"大于0。所以在这个例子中,它应该返回' test4'和' test6'

1 个答案:

答案 0 :(得分:0)

不要将标题转换为符号;使用select,而不是detect;以及一些小细节:

csv = CSV.read(report, :headers => true, :converters => :all).select do|row|
   row['number available'] > 0 && row['Score'] >=9}
end    

csv.each do |row|
  puts row["name"]
end