Skip to content

Instantly share code, notes, and snippets.

@julbouln
Last active October 19, 2024 06:54
Show Gist options
  • Save julbouln/516ab2be29d2921d4f4f3b4cb6211b4e to your computer and use it in GitHub Desktop.
Save julbouln/516ab2be29d2921d4f4f3b4cb6211b4e to your computer and use it in GitHub Desktop.
Malformed CSV fixer
class CSVFixer
attr_accessor :rows
def initialize(filename)
@rows = []
input_csv = CSV.open(filename)
loop do
begin
row = input_csv.shift
break unless row
@rows << row
#p row
rescue CSV::MalformedCSVError => e
puts "skipping bad row #{e}"
end
end
end
def save(filename)
CSV.open(filename, "w") do |csv|
@rows.each do |row|
csv << row
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment