Last active
October 19, 2024 06:54
-
-
Save julbouln/516ab2be29d2921d4f4f3b4cb6211b4e to your computer and use it in GitHub Desktop.
Malformed CSV fixer
This file contains 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
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