Importing a .txt file as string or finding expressions in said text file

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Importing a .txt file as string or finding expressions in said text file

Benji_123
Hi guys, I hope I've managed to set up this mailing list thing properly...

Anyway, I'm trying to import data from a text file, the data consists of a
letter followed by numbers encased in a d=" " phrase (for example d="M2 3q2
3 2 1") - in this case I'd like to read M 2 3 and Q 2 3 2 1 - the catch is
the sequence of letters and numbers isn't set or constant, and what's even
worse the number of numbers following each letter isn't set (so I can have
'c 1 2 3 4 q 1 2' or 'c 1 2 3 4 5 6 7 8 q 1 2')

Any idea how to even begin with this thing? Either I'm missing something or
there isn't even a function that would allow me to read the file as a string
so I could later work with it using strfind (which I started toying with
now, don't know yet if that even is going to work), but maybe Im missing
something?

TL;DR - can I import text file as string and/or find a certain pattern in an
irregular text file?

Thanks for any suggestions, Benji



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Importing a .txt file as string or finding expressions in said text file

Doug Stewart-4


On Mon, Apr 6, 2020 at 6:16 PM Benji_123 <[hidden email]> wrote:
Hi guys, I hope I've managed to set up this mailing list thing properly...

Anyway, I'm trying to import data from a text file, the data consists of a
letter followed by numbers encased in a d=" " phrase (for example d="M2 3q2
3 2 1") - in this case I'd like to read M 2 3 and Q 2 3 2 1 - the catch is
the sequence of letters and numbers isn't set or constant, and what's even
worse the number of numbers following each letter isn't set (so I can have
'c 1 2 3 4 q 1 2' or 'c 1 2 3 4 5 6 7 8 q 1 2')

Any idea how to even begin with this thing? Either I'm missing something or
there isn't even a function that would allow me to read the file as a string
so I could later work with it using strfind (which I started toying with
now, don't know yet if that even is going to work), but maybe Im missing
something?

TL;DR - can I import text file as string and/or find a certain pattern in an
irregular text file?

Thanks for any suggestions, Benji



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



Did you try this:



--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: Importing a .txt file as string or finding expressions in said text file

Benji_123
>Did you try this:
>https://octave.org/doc/v4.2.1/Line_002dOriented-Input.html

No, I did not, and this is pretty much exactly what I needed, thanks :)
Now I can just read line by line, checking first few characters if I found
the right line or end-of-file (used in while condition).
Now I only need to parse the data, but that shouldn't be too hard - in fact,
I already got it to work, though I feel in a pretty overcomplicated way, but
hey, it works :) And it's only done once per file so I don't care much about
performance here, as long as it works.

Thanks for help :3




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html