1
$\begingroup$

I am thinking about how can we import complex .txt file (Ansys/Abaqus) to MMA for the selected database?

For example: txt file (input file):

 ...
 ...
 10894, 10895, 10896, 10897, 10898, 10899, 10900, 10901, 10902, 10903, 10904, 10905, 10906, 10907, 10908, 10909
 10910, 10911, 10912, 10913, 10914, 10915, 10916, 10917, 10918, 10919, 10920, 10921, 10922, 10923, 10924, 10925
 10926, 10927
*Edfsf, cadifd=Saoot-23, coming
 10122,  10534,      1
*Gaaf, nlau=Saoot-24
   29,   30,   31,   32,   33,   34,  400,  401,  402,  403,  404,  405,  406,  407,  408,  409
  410,  411,  412,  413,  414,  415,  416,  439,  440,  441,  442,  443,  444,  445,  446,  447
 3157, 3158, 3159, 3160, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3168, 3169, 3170, 3171, 3172
 3173, 3174, 3175, 3176, 3177, 3178
*Edfsf, cadifd=Saoot-24, coming
 1477,  1782,     1
*Gaaf, nlau=Saoot-25
   32,   33,   76,   86,  455,  456,  457,  458,  459,  460,  461,  462,  463,  464,  465,  466
 9611, 9612, 9613, 9614, 9615, 9616, 9617, 9618, 9619, 9620, 9621, 9622, 9623, 9624, 9625, 9626
 9627, 9628, 9629, 9630
*Edfsf, cadifd=Saoot-25, coming
 8891,  9060,     1
*Gaaf, nlau=Saoot-26
   24,   25,   28,   29,   30,  344,  345,  346,  347,  348,  349,  350,  351,  352,  353,  354
  355,  356,  387,  388,  389,  390,  391,  392,  393,  394,  395,  396,  397,  398,  399,  400  
 ...
 ...

What do we need is such kind of output in MMA (matrix):

10122 10534 23
1477 1782 24
8891 9060 25  

Import rule is: we find *Edfsf line: the value after the key words Saoot- and the following line two values: namely 10122 10534 23 then we repeat the rule for the next same key words.

Sure, if I import all the data in MMA, it is ok, but how to import selected information in MMA, at least not clear for me now.

Thanks a lot for your support in advance

$\endgroup$
  • 2
    $\begingroup$ Is importing the entire file and then processing it acceptable or does the solution have to read the file line by line and filter/construct the required output? $\endgroup$ – Rohit Namjoshi Nov 20 '18 at 4:17
  • $\begingroup$ Where does this file come from? Can you add some background information? $\endgroup$ – xzczd Nov 20 '18 at 5:28
  • $\begingroup$ @RohitNamjoshi, the second option:the solution have to read the file line by line and filter/construct the required output. $\endgroup$ – ABCDEMMM Nov 20 '18 at 10:43
  • $\begingroup$ @xzczd , abaqus/ansys/codeaster and so on. $\endgroup$ – ABCDEMMM Nov 20 '18 at 10:43
  • $\begingroup$ Is it possible to export a more regular file (say, a binary file arranged according to a certain template) from these softwares? $\endgroup$ – xzczd Nov 20 '18 at 11:46
2
$\begingroup$

I know nothing about abaqus/ansys/codeaster, but I suspect the .txt file illustrated in the question isn't the best choice for exporting data from these softwares. Anyway, the following is a possible solution if you have to live with the .txt file:

read[stream_] := 
 Last@Last@Reap@(While[True, 
      Sow@{StringCases[If[# === EndOfFile, Break[], #] &@Find[stream, {"Edfsf"}], 
          "Saoot-" ~~ a__ ~~ "," :> ToExpression@a][[1]], 
        Read[stream, Number],
        Skip[stream, Word]; Read[stream, Number]}]; Close[stream])

The input of read function is a stream, which you can create with OpenRead function in your real case. Here I'll simply illustrate with StringToStream:

string = "*Edfsf, cadifd=Saoot-23, coming
   10122,  10534,      1
  *Gaaf, nlau=Saoot-24
     29,   30,   31,   32,   33,   34,  400,  401,  402,  403,  404,  405,  406,  407,  \
408,  409
    410,  411,  412,  413,  414,  415,  416,  439,  440,  441,  442,  443,  444,  445,  \
446,  447
   3157, 3158, 3159, 3160, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3168, 3169, 3170, \
3171, 3172
   3173, 3174, 3175, 3176, 3177, 3178
  *Edfsf, cadifd=Saoot-24, coming
   1477,  1782,     1
  *Gaaf, nlau=Saoot-25
     32,   33,   76,   86,  455,  456,  457,  458,  459,  460,  461,  462,  463,  464,  \
465,  466
   9611, 9612, 9613, 9614, 9615, 9616, 9617, 9618, 9619, 9620, 9621, 9622, 9623, 9624, \
9625, 9626
   9627, 9628, 9629, 9630
  *Edfsf, cadifd=Saoot-25, coming
   8891,  9060,     1";

stream = StringToStream@string;

read@stream
(* {{23, 10122, 10534}, {24, 1477, 1782}, {25, 8891, 9060}} *)
$\endgroup$
  • $\begingroup$ thanks a lot! however, "string = " this method can not be used for very large data (more than 5000 lines), can we use txt or other files to do this work? $\endgroup$ – ABCDEMMM Nov 24 '18 at 1:02
  • $\begingroup$ "string = "xxx.txt""??? $\endgroup$ – ABCDEMMM Nov 24 '18 at 1:07
  • $\begingroup$ @ABCDEMMM As mentioned above, please check the document of OpenRead. $\endgroup$ – xzczd Nov 24 '18 at 7:21

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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