TransWikia.com

How to import data from the file after one string and before another string?

Mathematica Asked on March 30, 2021

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
...

One Answer

One way to do it is

str = Import["file.dat", "Text"];
data = StringCases[str, "string 1n" ~~ 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 "rn" instead of just the "n".

Correct answer by LouisB on March 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP