Here's an example CSV file (Example 1):
"Text","”","False","False","False","False","0","1","24","0","9","450","450"
and the code to import it (Code 1):
Import[filepath, "CSV"]
(* Outputs: {{"Text", "", "False", "False", "False", "False", 0, 1, 24, 0, 9, 450, 450}} *)
Here's another file with different delimiters (Example 2):
"Text"|"”"|"False"|"False"
And here's the code to import it (from the answer here) (Code 2):
Import[filepath, "Table", "FieldSeparators" -> "|"]
(* Outputs: {{"Text", "â", "False", "False"}} *)
Either method does not seem to be correctly importing single or double quotation marks. In the example, the second value is being incorrectly imported. Weirdly enough, the following CSV file—which is just generated by copying the second value from the example—does work.
"”","”","”","”"
With different delimiters, this does not work again. What could be causing this unexpected behaviour? I tried the default CSV reader in Python and it doesn't seem to have any issues parsing the file.
Question Update
Based on MarcoB's comment, I realized that different file encodings are giving different results. Here are the following outputs of the example CSV files with different file encodings:
Example 1, ANSI:
{{"Text", "", "False", "False", "False", "False", 0, 1, 24, 0, 9, 450, 450}}
Example 1, UTF-8:
{{"Text", "\[CloseCurlyDoubleQuote]", "False", "False", "False",
"False", 0, 1, 24, 0, 9, 450, 450}}
Example 2, ANSI:
{{"Text", "", "False", "False"}}
Example 2, UTF-8:
{{"Text", "â", "False", "False"}}
In summary, the only CSV file which seems to be imported correctly is the Example 1 (comma separated values, no custom delimiters), encoded in UTF-8. All other cases do not seem to work. Python is able to import all the example cases without any problems. How do I make it work for all cases? I want to be able to just specify the delimiter and let Mathematica do the rest, like in Python.
Import
command, and the result was{{"Text", "\[CloseCurlyDoubleQuote]", "False", "False", "False", "False", 0, 1, 24, 0, 9, 450, 450}}
. That seems fine to me. What were you expecting? I am on Win10-64 and MMA 12. $\endgroup$"Text"|"”"|"False"|"False"
$\endgroup$CharacterEncoding
options or do you letImport
find out the encoding itself? I wouldn't have much hope thatImport
always gets this right automatically, if it even tries to (and I'm not sure whether it does or not)... $\endgroup$CharacterEncoding
.ANSI
appears to be a valid character encoding on the help page reference.wolfram.com/language/guide/CharacterOperations.html, but I do not have it listed under$CharacterEncodings
. Is this normal? I don't know which one to choose. $\endgroup$