从CSV文件中删除重复的行

时间:2017-04-04 11:51:49

标签: ruby

我想删除两列中具有相同值的CSV行。 CSV.open( "csv/competition-duped.csv", 'w' ) do | csv | CSV.read( file ).uniq{ | column | column.values_at( column[ 3 ], column[ 7 ] ) }.each do | row | csv << row end end 可以作为块传递,但我无法弄清楚:

CSV.read( file ).uniq{ | column | [ column[ 3 ], column[ 7 ] ] }.each do | row |

除此之外,我可能已找到解决方案。

x = [1:0.01:10];
y = [1:0.01:10];
figure
plot(sin([1:0.01:10]))
hold on
%// Save axes in variable
CurrentAxes = gca;
%// Pass it as argument to function
myplot(x,y,CurrentAxes)

1 个答案:

答案 0 :(得分:1)

您可以管理两个文件,即包含数据的主文件,以及您只需要编写所需数据的输出文件。

require 'csv'

main = CSV.read('csv/competition-duped.csv')
unwanted = nil

# Open the out file in write file mode
CSV.open('csv/out.csv', 'w') do |csv|
  # Add the headers of the main csv file
  csv << main.shift
  # Iterate for every row in your original csv file
  main.each do |data|
    # Check for duplicated data
    if data[0] != unwanted
      unwanted = data[0]
      # If isn't then write in the out file the data
      csv << data
    end
  end
end

我尝试用更短的方式做到这一点,我明白了:

# Open the out file in write file mode
CSV.open('csv/out.csv', 'w') do |csv|
  CSV.read('csv/competition-duped.csv').uniq.each { |r| csv << r }
end