Rectangle 27 0

ruby How to reformat CSV file to match proper CSV format?


require 'csv'
str= %q{abc,hello mahmoud,this is" description, bad}
puts str.split(',').to_csv 
#=> abc,hello mahmoud,"this is"" description", bad

Luckily, when working with invalid CSV, Rubys built-in methods will almost always be superior in every way. For example, parsing non-quoted fields is as easy as:

This would give you an array. If you really want valid CSV (f.e. because you rescued the MalformedCSVError) then there is... fasterCSV!

What you dont want to do is feed FasterCSV invalid CSV. Because of the way the CSV format works, its common for a parser to need to read until the end of the file to be sure a field is invalid. This eats a lot of time and memory.

just for clarification. fastercsv was an external library in ruby 1.8 and was then included as the csv standard library in ruby 1.9.

this fixes the unquoted problem but what if the csv file is malformed for another reason that can be fixed by openoffice also? Fore example: stackoverflow.com/questions/9098759/ Is there a generic solution for all these problems?

Note