Skip to content

Instantly share code, notes, and snippets.

@chrisb
Created January 21, 2013 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisb/4583193 to your computer and use it in GitHub Desktop.
Save chrisb/4583193 to your computer and use it in GitHub Desktop.
# 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