Support forum login

©2006-2012 IAAI Software

Contact Us Privacy Policy

CSV Field with a Comma and Double Quote

Submitted by jwp on Thu, 2010-03-11 05:38.

Hi David,

I have a CSV file with a field that includes both commas and double quotes. Magic Parser doesn't seem to be parsing the file correctly.

In my CSV file the fields that include commas are surrounded by double quotes. In a field that includes double quotes, the quotes have an extra set of double quotes around them.

The problem seems to occur when I have a comma and a double quote in the same field - e.g.

Favorite Movies: "Crash", "Gladiator", and "Titanic".

The CSV format puts double quotes in front of fields with commas, and an extra set of double quotes before and after quotes to differentiate them from end of text field delimiter. Here is what the CSV data looks like for this field:

"Favorite Movies: ""Crash"", ""Gladiator"", and ""Titanic""."

Let me know if that makes sense.

Best regards,
Jeremy

Submitted by support on Thu, 2010-03-11 10:02.

Hi Jeremy,

The double-quoted format is actually very difficult to process serially, so what I normally suggest is reading your CSV into a string (assuming that memory permits), and then pre-processing to replace double-double quotes with an alternative character - single quote for example; and then parsing as normal. For example:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
print_r($record);
  }
  
$csv file_get_contents("filename.csv"); // could be URL of course
  
$csv str_replace("\"\"","'",$csv);
  
MagicParser_parse("string://".$csv,"myRecordHandler","csv|44|0|34");
?>

Hope this helps,
Cheers,
David.

Submitted by jwp on Fri, 2010-03-12 01:37.

Great advice David! Thanks I'll give that a try.