Skip to content

Instantly share code, notes, and snippets.

@ka8725
Created April 26, 2012 12:13
Show Gist options
  • Save ka8725/2499187 to your computer and use it in GitHub Desktop.
Save ka8725/2499187 to your computer and use it in GitHub Desktop.
require "csv_corrector/version"
require "tempfile"
module CsvCorrector
def correct(file_path)
temp = Tempfile.new([File.basename(file_path, '.*'), '.csv'], File.dirname(file_path))
begin
File.open(file_path) do |file|
file.each do |line|
line.gsub!(/"(.*?)"\,/).each { |m| %Q["#{$1.gsub /"/, "'"}",] }
temp << line
end
end
temp.rewind
rescue
return false
ensure
temp.close
temp.unlink
end
true
end
module_function :correct
end
@ka8725
Copy link
Author

ka8725 commented Apr 26, 2012

require "csv_corrector/version"
require "tempfile"

module CsvCorrector
  def correct(file_path)
    temp = Tempfile.new([File.basename(file_path, '.*'), '.csv'], File.dirname(file_path))
    begin
      File.open(file_path) do |file|
        file.each do |line|
          line.gsub!(/"(.*?)"\,/).each { |m| %Q["#{$1.gsub /"/, "'"}",] }
          temp << line
        end
        file.write()
      end
      temp.rewind
      File.open(file_path, 'w') do |file|
        file.write(temp.read)
      end
    rescue
      return false
    ensure
      temp.close
      temp.unlink
    end
    true
  end

  module_function :correct
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment