开发者

uploading data using Numpy.genfromtxt with multiple formats

I have a file with a time stamp as a column, and numbers in all the rest. I can either load one or the other correctly, but not both. Frustrating the heck out of me...

This is what I am doing:

import numpy as np

file = np.genfromtxt('myfile.dat', skip_header = 1, usecols = (0,1,2,3), dtype = (str, float)开发者_如何学Python, delimiter = '\t')

So column 0 is the timestamp, and I want to read it in as a string. The rest I want to read in as floats. Does anyone know how to do this? I tried fooling around with names and dtypes, but I cannot get anything to work.

Thanks.


Perhaps try this:

import numpy as np

data = np.genfromtxt('myfile.dat',
                     skiprows=1,
                     usecols = (0,1,2,3),
                     dtype = '|S10,<f8,<f8,<f8',
                     delimiter = '\t')
print(data)
# [('2010-1-1', 1.2, 2.2999999999999998, 3.3999999999999999)
#  ('2010-2-1', 4.5, 5.5999999999999996, 6.7000000000000002)]

print(data.dtype)
# [('f0', '|S10'), ('f1', '<f8'), ('f2', '<f8'), ('f3', '<f8')]

print(data.shape)
# (2,)


If I have a tab-delimited file that looks like:

# Header Stuff
12:53:16    1.1111  2.2222  3.3333  4.4444
12:53:17    5.5555  6.6666  7.7777  8.8888
12:53:18    9.9999  10.0000 11.1111 12.1212

I think you can get what you're looking for by either specifying the dtype as None (so numpy chooses the dtypes for you):

file = np.genfromtxt('myfile.dat', skip_header = 1, usecols = (0,1,2,3,4),\
                       dtype = None, delimiter = '\t')

or you can set the dtypes explicitly:

file = np.genfromtxt('myfile.dat', skip_header = 1, usecols = (0,1,2,3,4), \
                     dtype=[('mytime','S8'),('myfloat1','f8'),('myfloat2','f8'),('myfloat3','f8')], \ 
                     delimiter = '\t')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜