Ruby-比较csv文件一列中的相邻条目
我是Ruby的新手,所以如果这很简单,请道歉:-) 我有一个5列的.csv文件。第一列有一个记录标识符(在本例中是一个驱动程序编号),每行中的其他4列有与该记录相关的数据。每个记录大约有50行数据(总共不到2000行)。.csv文件有一个标题行 我需要读取.csv文件并识别每个用户的最后一个条目,这样我就可以转到下一个用户。我试图让它比较第一列和下一行的条目 到目前为止,它返回的行号不正确,它们在1到5行之间Ruby-比较csv文件一列中的相邻条目,ruby,csv,Ruby,Csv,我是Ruby的新手,所以如果这很简单,请道歉:-) 我有一个5列的.csv文件。第一列有一个记录标识符(在本例中是一个驱动程序编号),每行中的其他4列有与该记录相关的数据。每个记录大约有50行数据(总共不到2000行)。.csv文件有一个标题行 我需要读取.csv文件并识别每个用户的最后一个条目,这样我就可以转到下一个用户。我试图让它比较第一列和下一行的条目 到目前为止,它返回的行号不正确,它们在1到5行之间 require 'csv-mapper' Given(/^I compare t
require 'csv-mapper'
Given(/^I compare the driver numbers from rows "(.*?)" to "(.*?)"$/) do |firstrow, lastrow|
data = CsvMapper.import('C:/auto_test_data/Courts code example csv.csv', headers: true) do
[dln]
end
row = firstrow.to_i
while row <= lastrow.to_i
@licnum1 = data.at(row).dln
@licnum2 = data.at(row+1).dln
if
@licnum2 == @licnum1
$newrecord = "same"
else
$newrecord = @licnum2
end
if
$newrecord != "same"
puts "Last row for #{@licnum1} is #{row}\n"
end
row = row + 1
end
end
我们将非常感谢您的帮助
谢谢
彼得这里有一种方法
current_identifier = nil
(firstrow.to_i..lastrow.to_i).each do |row|
if current_identifer != data.at(row).dln # current row is new identifier
if current_identifier # this is not the first row
puts "Last row for #{current_identifier} is #{row-1}\n"
end
current_identifier = data.at(row).dln # remember current row
end
# we need to track the last row as the last for the current identifier
puts "Last row for #{current_identifier} is #{lastrow.to_i}\n"
谢谢,先生们:-)为整理问题干杯,彼得。我终于到了。
current_identifier = nil
(firstrow.to_i..lastrow.to_i).each do |row|
if current_identifer != data.at(row).dln # current row is new identifier
if current_identifier # this is not the first row
puts "Last row for #{current_identifier} is #{row-1}\n"
end
current_identifier = data.at(row).dln # remember current row
end
# we need to track the last row as the last for the current identifier
puts "Last row for #{current_identifier} is #{lastrow.to_i}\n"