2

次のような構造を持つファイルを読み取る必要があります。

 1 2 3 4 5
 6 7 8 9 10
 11 22
 13 14 15 16 17
 18 19 20 21 22
 23 24

このファイルを単一の配列で読み取る必要があります = [ 1,2,3, ... , 23, 24]

numpyでそれを行う方法?? 使い方:

Array = np.genfromtxt(pathToFile, dtype=float, skip_header=1, comments='/')

うまくいきませんでした:

Line #796537 (got 2 columns instead of 5)
4

4 に答える 4

5

より簡単な方法:

result=np.fromfile(path_to_file,dtype=float,sep="\t",count=-1)
于 2012-10-15T18:55:32.050 に答える
2

np.fromstring代わりに使用しますか?

>>> np.fromstring(''.join(open('yourfile.txt', 'r').read().splitlines()),sep=" ")


array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,
        22.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,
        23.,  24.])

于 2013-05-24T14:42:07.350 に答える
0

なぜnumpyここが必要なのですか?

In [101]: with open('data1.txt') as f:
    lis=[float(y) for x in f for y in x.split()]
    print lis
   .....:     
   .....:     
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 22.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0]
于 2012-10-15T18:32:49.087 に答える
0

新しい行を削除して、別のファイルに保存します。

open('ofile.txt','w').write(''.join(open('infile.txt', 'r').read().splitlines()))

その後、動作します:

>>> np.genfromtxt('ofile.txt')
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,
    22.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,
    23.,  24.])
于 2013-05-24T14:31:06.843 に答える