0

I'm trying to extract data from a file that has columns with two different sizes. One of size 1, the other size 6. Unfortunately, I can't find a delimiter that works. The columns are not evenly spaced and neither are they the same size.

Example file:

1234
    123      456   -789
    24      -678      9
5678
    123      456   -789
    24      -678      9
9012
    123      456   -789
    24      -678      9
.
.
.

How would I extract this into an m x 6 array? I tried using '\t', '\r', and ' ', but I'm lost.

  • is there one number on every third row? – Joe Apr 01 '20 at 06:49
  • It might not be possible with numpy, so you have to put together a simple parser yourself. Should only be a few lines. https://www.geeksforgeeks.org/read-a-file-line-by-line-in-python/ create a list of lists and pass that to `np.array()`, should be the easiest way. – Joe Apr 01 '20 at 06:56
  • @Joe yes, there is only one number every third row. I was afraid I would have to do something funky. I remember looking at a way to only load every nth row, so I suppose I will have to do that. I was hoping there would be a better solution. – TylerSingleton Apr 01 '20 at 17:36
  • It's not that funky, should be just maybe twenty lines. You need `str.split()`, `str.strip()` and some cast like `float()` or `int()`. – Joe Apr 01 '20 at 18:16

0 Answers0