如何使用CSV.open和CSV.foreach方法转换csv文件中的特定数据?

时间:2015-09-21 06:09:55

标签: ruby csv

Old.csv文件包含这些标头"article_category_id", "articleID", "timestamp", "udid",但这些列中的某些值是字符串。所以,我试图将它们转换为整数并存储在另一个CSV文件New.csv中。这是我的代码:

require 'csv'
require 'time'

CSV.foreach('New.csv', "wb", :write_headers=> true, :headers =>["article_category_id", "articleID", "timestamp", "udid"]) do |csv|
    CSV.open('Old.csv', :headers=>true) do |row|

        csv['article_category_id']=row['article_category_id'].to_i
        csv['articleID']=row['articleID'].to_i
        csv['timestamp'] = row['timestamp'].to_time.to_i unless row['timestamp'].nil?

        unless udids.include?(row['udid'])
          udids << row['udid']
        end
        csv['udid'] = udids.index(row['udid']) + 1

        csv<<row
    end
end

但是,我收到以下错误:in 'foreach': ruby wrong number of arguments (3 for 1..2) (ArgumentError)

当我将foreach更改为open时,我收到以下错误:undefined method '[]' for #<CSV:0x36e0298> (NoMethodError)。这是为什么?我该如何解决?感谢。

3 个答案:

答案 0 :(得分:3)

CSV#foreach不接受文件访问权限作为第二个参数:

CSV.open('New.csv', :headers=>true) do |csv|
  CSV.foreach('Old.csv',
    :write_headers => true, 
    :headers => ["article_category_id", "articleID", "timestamp", "udid"]
  ) do |row|
    row['article_category_id'] = row['article_category_id'].to_i
    ...
    csv << row
  end
end

CSV#open应放在foreach之前。您将迭代旧的并生成新的。在循环内部,您应该更改row,然后将其附加到输出。

答案 1 :(得分:0)

您可以参考我的代码:

require 'csv'
require 'time'

CSV.open('New.csv', "wb") do |csv|
  csv << ["article_category_id", "articleID", "timestamp", "udid"]
  CSV.foreach('Old.csv', :headers=>true) do |row|
    array = []
    article_category_id=row['article_category_id'].to_i
    articleID=row['articleID'].to_i
    timestamp = row['timestamp'].to_i unless row['timestamp'].nil?

    unless udids.include?(row['udid'])
      udids << row['udid']
    end
    udid = udids.index(row['udid']) + 1
    array << [article_category_id, articleID, timestamp, udid]

    csv<<array
  end
end

答案 2 :(得分:0)

Vinh回答的问题是,最后的数组变量是一个内部有数组的数组。 那么插入indo CVS是什么样的     [[article_category_id,articleID,timestamp,udid]] 这就是为什么你得到双引号的结果。

请尝试这样的事情:

require 'csv'
require 'time'

CSV.open('New.csv', "wb") do |csv|
  csv << ["article_category_id", "articleID", "timestamp", "udid"]
  CSV.foreach('Old.csv', :headers=>true) do |row|
    article_category_id = row['article_category_id'].to_i
    articleID = row['articleID'].to_i
    timestamp = row['timestamp'].to_i unless row['timestamp'].nil?

    unless udids.include?(row['udid'])
      udids << row['udid']
    end
    udid = udids.index(row['udid']) + 1
    output_row = [article_category_id, articleID, timestamp, udid]

    csv << output_row
  end
end