2013-10-03 22 views
3

我有一個.txt文件,具有不同長度的行。每一行都是代表一個軌跡的連續點。由於每條軌跡都有其自己的長度,所以行的長度都不相同。也就是說,列的數量因行而異。Python中genfromtxt()的變量列數?

AFAIK,Python中的genfromtxt()模塊要求列數相同。

>>> import numpy as np 
>>> 
>>> data=np.genfromtxt('deer_1995.txt', skip_header=2) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1638, in genfromtxt 
    raise ValueError(errmsg) 
ValueError: Some errors were detected ! 
    Line #4 (got 2352 columns instead of 1824) 
    Line #5 (got 2182 columns instead of 1824) 
    Line #6 (got 1412 columns instead of 1824) 
    Line #7 (got 1650 columns instead of 1824) 
    Line #8 (got 1688 columns instead of 1824) 
    Line #9 (got 1500 columns instead of 1824) 
    Line #10 (got 1208 columns instead of 1824) 

It is also able to fill the missing values by the help of filling_values。但是,我認爲這會產生不必要的麻煩,我希望避免。

那麼,什麼是最好的(Pythonic)的方式來簡單地導入這個數據集在沒有填寫「缺失值」?

回答

2

Numpy.genfromtxt不處理可變長度的行,因爲numpy只能處理數組和矩陣(固定的行/列大小)。

您需要手動解析數據。例如:

數據(CSV爲主):

0.613 ; 5.919 
0.615 ; 5.349 
0.615 ; 5.413 
0.617 ; 6.674 
0.617 ; 6.616 
0.63 ; 7.418 
0.642 ; 7.809 ; 5.919 
0.648 ; 8.04 
0.673 ; 8.789 
0.695 ; 9.45 
0.712 ; 9.825 
0.734 ; 10.265 
0.748 ; 10.516 
0.764 ; 10.782 
0.775 ; 10.979 
0.783 ; 11.1 
0.808 ; 11.479 
0.849 ; 11.951 
0.899 ; 12.295 
0.951 ; 12.537 
0.972 ; 12.675 
1.038 ; 12.937 
1.098 ; 13.173 
1.162 ; 13.464 
1.228 ; 13.789 
1.294 ; 14.126 
1.363 ; 14.518 
1.441 ; 14.969 
1.545 ; 15.538 
1.64 ; 16.071 
1.765 ; 16.7 
1.904 ; 17.484 
2.027 ; 18.36 
2.123 ; 19.235 
2.149 ; 19.655 
2.172 ; 20.096 
2.198 ; 20.528 
2.221 ; 20.945 
2.265 ; 21.352 
2.312 ; 21.76 
2.365 ; 22.228 
2.401 ; 22.836 
2.477 ; 23.804 

解析器:

import csv 
datafile = open('i.csv', 'r') 
datareader = csv.reader(datafile) 
data = [] 
for row in datareader: 
    # I split the input string based on the comma separator, and cast every elements into a float 
    data.append([ float(elem) for elem in row[0].split(";") ]) 

print data 
+0

'data'仍然會具有可變長度的行儘管... –

+0

是的。無論如何,嘗試從可變長度的行輸入創建固定長度的行是很奇怪的:它可能表明模型是錯誤的 – lucasg