(MMA v11.1.1)
I have many empty fields in my CSV files and I couldn't understand why sometimes SemanticImport
just failed to recognize the headers. I just find out that the problem is when the last column is ALL empty
Here is a minimal example:
(for the purpose i use SemanticImportString
with a string, instead of SemanticImport
with a file, but the results are the same)
SemanticImportString["a,b,c\n1,2,\n11,22,\n111,222,"]
whereas if just one element exists in the last column, all works fine:
SemanticImportString["a,b,c\n1,2,\n11,22,33\n111,222,"]
Trying to trick the process by specifying explicit delimiters like this produce an error:
SemanticImportString["a,b,c\n1,2,\n11,22,\n111,222,", Delimiters -> {",", ",\n"}]
The only workaround i found is adding an additionnal comma at the end of the lines:
SemanticImportString["a,b,c\n1,2,\n11,22,\n111,222," //
StringReplace[#, ",\n" -> ",,\n"] &]
In comparison Import
(or ImportString
) works fine:
ImportString["a,b,c\n1,2,\n11,22,\n111,222,"]
{{"a", "b", "c"}, {1, 2, ""}, {11, 22, ""}, {111, 222, ""}}
and Export
(ExportString
) of a table with an empty last column show that rows are just terminated with a comma, and that this format should be handled by mathematica:
ExportString[{{a, b, c}, {1, 2,}, {11, 22,}}, "CSV"] // InputForm
"a,b,c\n1,2,\n11,22,"
So, I would say it is a bug ...
Any other workaround than adding (programmatically or manually) an additional comma to all the problematic files ?