1
$\begingroup$

Consider a file file.dat that has a form

...
string 1
x1 y1 z1
x2 y2 z2
...
string 2
...

where x,y,z are numbers.

Could you please tell me how to import only the following strings in the form of numerical data?

x1 y1 z1
x2 y2 z2
...
$\endgroup$

1 Answer 1

2
$\begingroup$

One way to do it is

str = Import["file.dat", "Text"];
data = StringCases[str, "string 1\n" ~~ x___ ~~ "\nstring 2" :>
    ImportString[x, "Table"]]

This example includes the newline character explicitly. You may or may not want to include one before the first "string" and after the "2". Or, you may want to leave the newline out. Note that some text files may terminate the lines with "\r\n" instead of just the "\n".

$\endgroup$
2
  • $\begingroup$ :> ImportString[x, "Table"] or something I guess since OP asked to import the numbers "in the form of numerical data". $\endgroup$
    – C. E.
    Jul 30, 2020 at 10:11
  • $\begingroup$ Good catch, @C.E. Many thanks. $\endgroup$
    – LouisB
    Jul 30, 2020 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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