Tuesday, 10 September 2013

ruby tsv scraper (if line contains x, then save)

ruby tsv scraper (if line contains x, then save)

I want to open a tsv file, and save specific rows to a new csv file.
If the row contains 'NLD' in a field with the header 'Actor1Code', I want
to save the row to a csv; if not, I want to iterate to the next row. This
is what I have so far, but apparently that is not enough:
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

No comments:

Post a Comment