Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]

Due credit.. borrowed from here : http://mathematica.stackexchange.com/a/103030/2079https://mathematica.stackexchange.com/a/103030/2079

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]

Due credit.. borrowed from here : http://mathematica.stackexchange.com/a/103030/2079

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]

Due credit.. borrowed from here : https://mathematica.stackexchange.com/a/103030/2079

added 86 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]

Due credit.. borrowed from here : http://mathematica.stackexchange.com/a/103030/2079

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]

Due credit.. borrowed from here : http://mathematica.stackexchange.com/a/103030/2079

added 378 characters in body
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Here is a start..

Export["test.txt", "a1,b1,c1,d1,\"e1\ne1\ne1\"\na2,b2,\"c2\nc2\nc2\",d2,e2"]

i = 0;
StringSplit[StringJoin[#], ","] & /@ 
  Split[Characters[Import["test.txt", "Text"]],
   (If[# == "\"", ++i]; ! (# == "\n" && EvenQ[i]) ) &]

Note this will break if there are commas within quoted strings.

Edit: a bit more elegant..and this takes care of the comma-within-quotes issue.

stringsplitnoquote[string_String, d_String /; StringLength[d] == 1] :=
  Module[{i = 1},
  StringSplit[
   string, _?((If[# == "\"", i *= -1]; i == 1 && # == d) &)]]

stringsplitnoquote[#, ","] & /@ 
     stringsplitnoquote[ Import["test.txt", "Text"] , "\n"]
Source Link
george2079
  • 39.1k
  • 1
  • 44
  • 111
Loading