Created
January 21, 2013 02:22
-
-
Save chrisb/4583193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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