0
$\begingroup$

My dataset is like:

id, feat_1, feat_2, feat_8, feat_9, feat_10, feat_11, feat_17, feat_18, feat_22, feat_2, target

1, 1, 0, 0, 0, 0, 1, 2, 0, 1, 0, Class_1
2, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, Class_1

The file is very large, so I don't want to use 'Import". Therefore I would like to use 'ReadList'

strm = OpenRead["test.TXT"];
Skip[strm, String, 1];
dataM = ToExpression@
  ReadList[strm, Table[Record, {12}], NullRecords -> True, 
   RecordSeparators -> {",", "\n"}]
Close[strm];

The output is: {1, 1, 0, 0, 0, 0, 1, 2, 0, 1, 0, Class_}

The integer in 'Class_1" disapeared. If I don't use 'ToExpression@' then all integers are seen as strings ("1", "1"....)

Anyone a suggestion how to solve this issue

$\endgroup$
  • $\begingroup$ The underscore _ as a pattern matching mechanism is reserved for mathematica. $\endgroup$ – egwene sedai Mar 20 '15 at 15:46
2
$\begingroup$

The underscore _ as a pattern matching mechanism is reserved for mathematica. Therefore, either use something like

dataM = ReadList[strm, Table[Record, {12}], NullRecords -> True, 
RecordSeparators -> {",", "\n"}];

output = Table[Flatten[{ToExpression[Drop[dataM[[i]], -1]], Last[dataM[[i]]]}],
{i,1,Length[dataM]}];

or remove the underscore in your data file.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.