2015-01-31 63 views
1

我有一個excel文件/ csv同時具有列和行標題(第1行是所有標題,列A是所有行標題)。當我提供列和行時,我希望使用dictreader返回(x,y)座標的值。給出列標題和行的值時的單元格值

最後,我開始能夠給出多個列和一個單行,並且它將合併該行的每個給定列中的值。但我會從嬰兒的步驟開始,因爲我目前甚至無法返回我想要的第一個值。這裏是我的Excel文件/ CSV的小樣本:

PinName RF_Switch_TX1 RF_Switch_TX2 RF_Switch_TX3 RF_Switch_TX3_Scope1 RF_Switch_TX3_Scope2 

DM_D_0 1255,1266,1311         1154,1105, 
DM_D_1 1256,1266,1311         1154,1105, 
DQS      1101,1161  1105         1153,1105 

如何建立一個功能,如果提供的PIN名稱「DM_D_1」和列標題「RF_Switch_TX3_Scope1」它會返回1154,1105,

我希望只是使用dictreader,但我需要建立一個迭代函數來搜索我的文件嗎?

回答

2

會使用Pandas是一種可接受的方法嗎? (最初的問題提到了Python,但現在看來並不是這樣。)我不確定這是熊貓最習慣的用法,但它似乎是做你想做的事情。

數據

我放到一個CSV文件這一點。

PinName,RF_Switch_TX1, RF_Switch_TX2,RF_Switch_TX3,RF_Switch_TX3_Scope1,RF_Switch_TX3_Scope2 
DM_D_0,"1255,1266,1311",,,"1154,1105,", 
DM_D_1,"1256,1266,1311",,,"1154,1105,", 
DQS,,"1101,1161",1105,,"1153,1105" 

一些代碼

from pandas import read_csv 

df = read_csv("/Users/igow/Desktop/so_data.csv") 
df = df.set_index(['PinName']) 

def get_value(row, col): 
    return df[col][row] 

print(get_value(col='RF_Switch_TX3_Scope1', row='DM_D_1')) 
0

如果你把數據以CSV或至少正確指定分隔符,那麼你就可以做到以下幾點:

In [56]: q = StringIO('''PinName,RF_Switch_TX1, RF_Switch_TX2,RF_Switch_TX3,RF_Switch_TX3_Scope1,RF_Switch_TX3_Scope2 
    ....: DM_D_0,"1255,1266,1311",,,"1154,1105,", 
    ....: DM_D_1,"1256,1266,1311",,,"1154,1105,", 
    ....: DQS,,"1101,1161",1105,,"1153,1105"''') 

In [57]: df1 = pd.read_csv(q,) 

In [58]: df1.loc[df1['PinName'] == 'DM_D_1']['RF_Switch_TX3_Scope1'].values[0] 
Out[58]: '1154,1105,' 

In [59]: 
相關問題