0

我想打开一个 TSV(制表符分隔值)文件,并将特定行保存到一个新的 CSV(逗号分隔值)文件中。

如果该行包含'NLD'在标题为“Actor1Code”的字段中,我想将该行保存到 CSV;如果没有,我想迭代到下一行。这是我到目前为止所拥有的,但显然这还不够:

require 'csv'

CSV.open("path/to.csv", "wb") do |csv| #csv to save to
  CSV.open('data.txt', 'r', '\t').each do |row| #csv to scrape
    if row['Actor1Code'] == 'NLD'
      csv << row
    else
    end
  end
end
4

1 回答 1

3

Are you sure that you're calling CSV.open correctly? The documentation seems to suggest arguments are passed in as hashes:

CSV.open('data.txt', 'r', col_sep: "\t")

The error you're seeing is probably the result of '\t' being interpreted as a hash and referenced with [].

于 2013-09-10T15:00:27.857 回答