3
$\begingroup$

I have to read and process a file that looks like this:

 MASSA TMASS1
 uscita da elettrodi:
            0         1   0.56705 -19.98160   2.80000  -0.87939   0.66823  -0.63034   0.39513
 ingresso tmass1:
            0         1   0.56705 -19.13351   2.00000  -0.37791   0.66823  -0.63034   0.39513
 MASSA TMASS
 uscita da elettrodi:
            0         1   2.10543  17.20236  -1.57617  -3.40000  -0.97477  -0.07910   0.20872
 MASSA TMASS1
 uscita da elettrodi:
            0         7   0.00018 -18.08245  -1.30564   3.40000   0.57294  -0.75691  -0.31437

As you can see there are both text and numbers, numbers must be put into two lists depending on the fact that in the above line it says MASSA TMASS1 of MASSA TMASS and the lines that say uscita da elettrodi: or ingresso tmass1: must be skipped.

How can I read it?

EDIT For the above example I expect an output like this:

{{0         1   0.56705 -19.98160   2.80000  -0.87939   0.66823  -0.63034   0.39513},{0         1   0.56705 -19.13351   2.00000  -0.37791   0.66823  -0.63034   0.39513},{0         7   0.00018 -18.08245  -1.30564   3.40000   0.57294  -0.75691  -0.31437}}

{0         1   2.10543  17.20236  -1.57617  -3.40000  -0.97477  -0.07910   0.20872}
$\endgroup$
0

2 Answers 2

2
$\begingroup$

A simple way is to Import the file and use Position to locate the first marker text "MASSA". The locations can be used separate the file contents into groups. Each group will contain data for only one of the markers. The groups are then read looking at the second marker text "TMASS1" or "TMASS". For example,

fname = "flatfile"
raw = Import[fname, "Table"];
raw // ColumnForm;

markers = Append[Flatten@Position[raw, {"MASSA", _}], Length[raw] + 1];

groups = raw[[#]] & /@ Span @@@ Transpose[{Most[markers], Rest[markers] - 1}];
groups // ColumnForm;

data1 = Flatten[Cases[groups, {{_, "TMASS1"}, x___} :>
                Cases[{x}, {_Integer, ___}]], 1];
data2 = Flatten[Cases[groups, {{_, "TMASS"}, x___} :>
                Cases[{x}, {_Integer, ___}]], 1];

data1  (* {{0, 1, 0.56705, -19.9816, 2.8, -0.87939, 0.66823, -0.63034, 0.39513}, 
           {0, 1, 0.56705, -19.1335, 2., -0.37791, 0.66823, -0.63034, 0.39513},
           {0, 7, 0.00018, -18.0825, -1.30564, 3.4, 0.57294, -0.75691, -0.31437}} *)

data2  (* {{0, 1, 2.10543, 17.2024, -1.57617, -3.4, -0.97477, -0.0791, 0.20872}} *)
$\endgroup$
2
$\begingroup$

Not sure how it scales but maybe will do:

Import[path, "Table"] //
  Split[#, ! MatchQ[#2[[2]], "TMASS1" | "TMASS"] &] & //
  GroupBy[#, #[[1, 2]] &, DeleteCases[{__String}]@*Apply[Join]] &

<|
    "TMASS1" -> {
        {0, 1, 0.56705, -19.9816, 2.8, -0.87939, 0.66823, -0.63034, 0.39513},
        {0, 1, 0.56705, -19.13351, 2., -0.37791, 0.66823, -0.63034, 0.39513},
        {0, 7, 0.00018, -18.08245, -1.30564, 3.4, 0.57294, -0.75691, -0.31437}
    },
    "TMASS" -> {{0, 1, 2.10543, 17.20236, -1.57617, -3.4, -0.97477, -0.0791, 0.20872}}
 |>
$\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.