Vedant
Vedant

Reputation: 523

Making a numpy array with a specific data format

I have a text file which contains a log like this:

245     (click,view,view,check,view,view)         (22,1,5,6,7,15)

305     (view,run)       (5,6)

3051         (run,run)      (115,36)

It is tab seperated, the number of entries in the second column field is equal to the third column field.

I am finding it difficult to take this into python using np.loadtxt as it has a delimiter and text and brackets (circular) to separate the 2nd fields and 3rd fields.

Looking forward for suggestions on how I should go about it?

Upvotes: 1

Views: 63

Answers (1)

DHandle
DHandle

Reputation: 402

Perhaps it'd be best to parse it in some way with standard python first, before loading into numpy? Without knowing a lot about how numpy needs this to be formatted, below is an example to turn it into an array of arrays.

For instance you could use split().

Def MakeItAList(lineFromFile): list = split(lineFromFile, " ") list[1] = split(list[1][1:-1],",") list[2] = split(list[2][1:-1],",")

It's a bit clunky but it will turn 245 (click,view,view,check,view,view) (22,1,5,6,7,15). into something like [245, [click,view,view,check,view,view], [22,1,5,6,7,15]] which is a little more manageable as a data-structure.

Upvotes: 2

Related Questions