2013-10-27 31 views
1

我有一个整数数据的文件,其中前几行/列用于名称。get genfromtxt/loadtxt忽略被忽略的列/行中的数据类型

我希望能够使用genfromtxtloadtxt,并仍然得到numpy读取它作为一个同源阵列。为此,我使用了选项skiprowsusecols,但它没有帮助。 在(工作)下面的例子,我期望print(test_array.shape)给(3,3)和print(test.array)

[[0 0 0] 
[0 1 0] 
[1 0 0]] 

有没有什么方法可以达到我想要的东西,而不与UNIX修整第一行/列工具在尝试加载文件之前?请注意,我要加载的实际文件是B-I-G(〜6演出),因此任何解决方案都不应该计算量太大。

from __future__ import print_function 
from StringIO import StringIO #use io.StringIO with py3 
import numpy as np 

example_file = StringIO("FID 1 2 3\n11464_ATCACG 0 0 0\n11465_CGATGT 0 1 0\n11466_TTAGGC 1 0 0") 
test_array = np.loadtxt(example_file, skiprows=1, usecols=(1,), dtype=int) 

print(test_array.shape) #(3,) 
print(test_array) #[0 0 1] 

回答

1

可以在np.genfromtxt使用usecolsskip_header标志。然后它可以正常工作:

test_array = np.genfromtxt(example_file, skip_header=1, usecols=(1,2,3)) 
>>> print(test_array) 
[[ 0. 0. 0.] 
[ 0. 1. 0.] 
[ 1. 0. 0.]]