I have been struggling with this basic task, and I am surprised it is this hard to do in Mathematica. I get lost in the maze of Import and options and sub-options and after trying many things, still not able to figure how to do it.
There are few questions on this, but none of them have the answer that works for me. Here is the problem. I have plain text file. Each line has this mixed format
The strings are quoted, and a string can also have commas and escaped "\" characters in it. So reading the line as string and using
StringSplit on comma and such, does not really work.
Here is example of the file. 2 lines only: (call it test.txt)
9,1,"",19,0.0151,"This , is string one"," this is \\ another string" 10,2,"note",22,0.0199,"This , also string"," and this too"
I need to read this file into Mathematica, so I can process each line.
Clearly, the result of the import should be a list of lists. Each one of the lists represent one line. This is what I tried
data = Import["test.txt", "Table"]
The problem with this, is that each line in one full string!
data[] // FullForm
So one has to parse out the entries out, which does not work, since strings can has commas in them also. So can't use
StringSplit and use "," as separator.
2) Looked at supported elements in file
data = Import["test.txt", "Elements"]
Plaintext does not work. It reads the whole file as one string. Same as using
Lines does not work. Same problem. Same as
3) tried to use CVS. renamed the file to CVS, but Mathematica did not like it.
data = Import["test.cvs", "CVS"]
3) tried "TSV"
data = Import["test.txt", "TSV"]
but again, each list is actually one string. Back to square one.
how-to-read-a-list-from-a-file-which-has-mixed-types-of-data with answer there that says
It seems there is no ready way to read in this mixed type data. The answer there does not work, since it uses
StringSplit on commas, which does not work in this case.
And others, but none of them really have a solution to this basic issue.
In Matlab, this is trivial to do. Matlab has textscan which I use for everything.
fileID = fopen('test.txt','r'); data = textscan(fileID,'%d %d %q %d %f %q %q','Delimiter',','); fclose(fileID);
Anyone knows how to do the same in Mathematica? Using 10.3.1 on windows 7.