36145

How to reformat CSV file to match proper CSV format

Question:

I have a web application that parse users uploaded csv files.

Some users upload csv files don't match proper csv format mentioned <a href="http://creativyst.com/Doc/Articles/CSV/CSV01.htm" rel="nofollow">here</a>

For example:

abc,hello mahmoud,this is" description, bad

This should be

abc,hello mahmoud,"this is"" description", bad

When I used ruby <strong>fastercsv</strong> library to parse the wrong csv, it <strong>fails</strong>. However, it <strong>success</strong> when I open the file by <strong>excel or openoffice</strong>.

Is there any ruby library can reformat the csv text to put it in a proper format?

Answer1:

From the <a href="http://fastercsv.rubyforge.org/" rel="nofollow">docs</a>:

<blockquote>

What you don‘t want to do is feed FasterCSV invalid CSV. Because of the way the CSV format works, it‘s 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.

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

data.split(",")

</blockquote>

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

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

Recommend

  • Graceful pod termination
  • Fat binaries in iOS
  • How do I capture a rsolr Sunspot exception raised on a different thread from a model callback?
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • How does the HEXTORAW() function work? What is the algorithm?
  • What does certain JVM do after loading ByteCode into memory?
  • SSL client cert authentication for only some URLs?
  • SIP API media codecs
  • jquery validation - waiting for remote check to complete
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Facebook iOS SDK Not Calling Completion Handler
  • VSCode change debug shell to bash on windows
  • How to 'create temp table as select' in Slick?
  • npm 5.4.1 install/uninstall all failing
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • JqueryMobile Popup menu is not working
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • What is the purpose of TaskExecutor in spring?
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Zurb Foundation _global.scss meta styles for js?
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Is it possible to access block's scope in method?
  • Meteor: Do Something On Email Verification Confirmation
  • Get data from AJAX - How to
  • Bug in WPF DataGrid
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Running a C# exe file
  • Where to put my custom functions in Wordpress?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Why winpcap requires both .lib and .dll to run?
  • QuartzCore.framework for Mono Develop
  • RestKit - RKRequestDelegate does not exist
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • WPF Applying a trigger on binding failure
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • reshape alternating columns in less time and using less memory