parsing tab separated data efficiently into numpy/pylab arrays

P

per

hi all,

what's the most efficient / preferred python way of parsing tab
separated data into arrays? for example if i have a file containing
two columns one corresponding to names the other numbers:

col1 \t col 2
joe \t 12.3
jane \t 155.0

i'd like to parse into an array() such that i can do: mydata[:, 0] and
mydata[:, 1] to easily access all the columns.

right now i can iterate through the file, parse it manually using the
split('\t') command and construct a list out of it, then convert it to
arrays. but there must be a better way?

also, my first column is just a name, and so it is variable in length
-- is there still a way to store it as an array so i can access: mydata
[:, 0] to get all the names (as a list)?

thank you.
 
M

Matteo

hi all,

what's the most efficient / preferred python way of parsing tab
separated data into arrays? for example if i have a file containing
two columns one corresponding to names the other numbers:

col1    \t     col 2
joe    \t  12.3
jane   \t 155.0

i'd like to parse into an array() such that i can do: mydata[:, 0] and
mydata[:, 1] to easily access all the columns.

right now i can iterate through the file, parse it manually using the
split('\t') command and construct a list out of it, then convert it to
arrays. but there must be a better way?

also, my first column is just a name, and so it is variable in length
-- is there still a way to store it as an array so i can access: mydata
[:, 0] to get all the names (as a list)?

thank you.

I think you can do it through:

array.fromfile()
array.reshape()

but you should look up the reference for those.
 
M

mapb81

You could take a look/use the very handy csv2rec function in
matplotlib.mlab, which creates numpy struct arrays.

Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top