Skip to content

Instantly share code, notes, and snippets.

@chrisb
Created Jan 21, 2013
Embed
What would you like to do?
# untained_csv_each(File.open('my.csv'),'Date') do |row|
# # do something with row
# end
def untainted_csv_each(file,line_match,&block)
tmp_file = File.open(file.path+"-tmp","w+")
garbage_passed = false
file.each do |line|
garbage_passed = true if line =~ /^"(.*)#{line_match}"/ && !garbage_passed
tmp_file.write(line) if garbage_passed
end
tmp_file.close
begin
CSV.foreach(tmp_file.path,{ :headers => true }).each(&block)
rescue CSV::MalformedCSVError
# ignore
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment