I can propose a more general solution. It is based on regular expressions (or here, string patterns). The idea is to look for the shortest strings in the file that begins with double-quote, ends with double quotes, does not contain any double quote, but can contain one or many EndOfLine. As it is a CSV, there are comma before the double-quote (thus, this solution cannot be applied if the double-quote string can be the first field in the input).
First, lets define a string pattern with any text or EndOfLine:
textenter = Repeated[Except["\""] .. ~~ EndOfLine];
It can contain any number of CRLF as long as some text preceeds them.
The second step is to locate all the substrings in a string variable, say csvstring
, that match a comma, a double-quote, a textenter pattern, some final text before another double-quote:
u = StringCases[cvsstring,{Shortest[",\"" ~~ x : textenter ~~ y___ ~~ "\""]}];
Next, replace all the occuences found in u
with the same but the CRLF replaced by some character (here I used [Wolf] just for the sake of the example):
v = StringReplace[cvsstring, Map[# :> StringReplace[#, "\n" -> "\[Wolf]"] &, u]];
This is it; convert v
as any csv text string containing strings delimited with double quote with:
ImportString[v, "CSV", "TextDelimiters" -> "\""]
This can be made into a function with:
RemoveInnerCRLF[cvsstring_] := Module[{u, v},
u = StringCases[cvsstring,{Shortest[",\"" ~~ x : textenter ~~ y___ ~~ "\""]}];
v = StringReplace[cvsstring, Map[# :> StringReplace[#, "\n" -> "\[Wolf]"] &, u]];
ImportString[v, "CSV", "TextDelimiters" -> "\""]
]
which you can use with
reg = RemoveInnerCRLF[Import[myfile, "Text"]];
where myfile
is some indication of a file that needs to be imported.
Import["filename.csv","CSV"]
$\endgroup$