2015-04-07 63 views
0

我有一個數據文件,其中包含幾個參數,其中一些參數是顏色值,它們是RGB格式,即每個顏色單元格中都有數組(R,G,B) 。numpy genfromtxt讀取多個值的單元格

細胞的其餘部分包含一個單一的值,這導致錯誤

Line #11175 (got 38 columns instead of 47) 

這是數據文件(每個值與製表符分隔)的樣品:

RightValidity Vergence FixationDist EventTimeStamp EventName EventType EventId Code Parameters Value for trial 1 Value for trial 2 Value for trial 3 
4 3.5522 0.613 1537.011 InputEvent Mouse_DW 2 999 aperture yes/no 1 1 1 
4 3.5522 0.613 1736.592 InputEvent Mouse_UP 2 999 aperture color (0.8, 0.8, 0.8) (0.8, 0.8, 0.8) (0.8, 0.8, 0.8) 
4 3.5522 0.613 1752.87 TrialEvent 0 START 8 aperture division 3 3 3 

我讀的數據是data = np.genfromtxt(file),其中file是上面的數據。

我該如何解決這個問題?有沒有辦法從單元中讀取數組?

+2

如果您發佈了**實際**數據和**代碼**,以便其他人可以*嘗試*重現您的錯誤 – EdChum

+0

好吧,我現在將編輯,謝謝。 –

+0

查找有關讀取帶空格的引用字符串的問題。例如,嘗試用'()'作爲引號的python'csv'閱讀器。或者預處理該行,將有問題的字符串轉換爲'genfromtxt'可以處理的字符串值。 – hpaulj

回答

0

我認爲將數據存儲爲numpyarray是沒有意義的。您是否混合了數據類型,不僅在列之間,還在某些列中。 pandasdataframe自然是更適合這種類型的事情:

唯一的步驟所需,超越了簡單read_csv,僅僅是被讀取爲字符串回到使用元組的元組.map(eval)轉換:

In [20]: 
import pandas as pd 
df = pd.read_csv('temp.txt', sep='\t') 
In [21]: 

print df 
    RightValidity Vergence FixationDist EventTimeStamp EventName EventType \ 
4   3.5522  0.613  1537.011  InputEvent Mouse_DW   2 
4   3.5522  0.613  1736.592  InputEvent Mouse_UP   2 
4   3.5522  0.613  1752.870  TrialEvent   0  START 

    EventId Code   Parameters Value for trial 1 Value for trial 2 \ 
4   999 aperture yes/no     1     1 
4   999  aperture color (0.8, 0.8, 0.8) (0.8, 0.8, 0.8) 
4    8 aperture division     3     3 

    Value for trial 3 
4     1 
4 (0.8, 0.8, 0.8) 
4     3 
In [22]: 

df['Value for trial 1'].values #data not stored in tuples 
Out[22]: 
array(['1', '(0.8, 0.8, 0.8)', '3'], dtype=object) 
In [24]: 

df['Value for trial 1'].map(eval).values #so we convert the tuples to tuples, leave numerical numbre unchanged 
Out[24]: 
array([1, (0.8, 0.8, 0.8), 3], dtype=object) 
In [25]: 

df['Value for trial 1'] = df['Value for trial 1'].map(eval) 
df['Value for trial 2'] = df['Value for trial 2'].map(eval) 
df['Value for trial 3'] = df['Value for trial 3'].map(eval)