2014-11-25 66 views
0

我正在處理包含多列的製表符分隔文件。每列包含超過3000條記錄。如何將列值存儲在變量中

Column1  Column2 Column3  Column4 
1000041  11657 GenNorm  albumin 
1000043  24249 GenNorm  CaBP 
1000043  29177 GenNorm  calcium-binding protein 
1000045  2006  GenNorm  tropoelastin 

問題:使用Python,如何讀取的標籤分離文件,並在一個單一的變量中的每個柱(用其記錄)存儲。使用「打印」打印出特定的列(S)

初步代碼:我用這個代碼到目前爲止讀取TSV文件

import csv 
Dictionary1 = {} 

with open("sample.txt", 'r') as samplefile: 
     reader = csv.reader(samplefile, delimiter="\t") 
+0

問題。鑑於5線輸入,你想要輸出什麼? – abarnert 2014-11-25 02:46:37

+0

我編輯了這個問題......不要注意點... – MEhsan 2014-11-25 02:49:43

+0

你編輯的問題仍然沒有告訴我們你想要的輸出是什麼。 – abarnert 2014-11-25 02:51:40

回答

1

我覺得你剛纔問如何「轉置「CSV文件從一系列的行到列的序列。

在Python中,你可以隨時使用zip功能轉iterables的可迭代:

with open("sample1.txt") as samplefile: 
    reader = csv.reader(samplefile, delimiter="\t") 
    columns = zip(*reader) 

現在,如果你想以打印每列:

for column in columns: 
    print(column) 

這裏,columns是元組的迭代器。如果你想要一些其他格式,比如將列名映射到值列表,你可以很容易地進行轉換。例如:

columns = {column[0]: list(column[1:]) for column in columns} 

或者,如果你想將它們放在四個獨立的變量,你可以使用普通的元組拆包:

col1, col2, col3, col4 = columns 

但並不似乎是一個很好的理由要做到這一點。

+1

我剛剛意識到我的答案並沒有解釋這一點,所以:如果你去了最後一個版本,你以後想要關聯兩列彼此(因爲你的意見暗示),這是'zip'的另一份工作。例如,'zip(col1,col4)'給你一個像'('100041','albumin')'對的迭代,而'dict(zip(col4,col1))'給你一個映射,你可以看看'白蛋白'並獲得'100041'。 – abarnert 2014-11-25 19:20:41

-1

不確定在Python中的代碼,但使用此循環。一旦你把所有東西都存入字典中,然後使用這個循環,然後使用該函數調用索引來打印你可以修改函數的方法,以適應你想要的密鑰,你可以通過一個單詞來搜索等。

int mainCounter = 0; 
int counter1 = 0; 
string arrColumn1[3000]; 

int counter2 = 0; 
string arrColumn1[3000]; 

int counter3 = 0; 
string arrColumn1[3000]; 

int counter4 = 0; 
string arrColumn1[3000]; 

for(int i = 0; i<dictionary.length; ++i){ 
     switch (mainCounterounter) 
     { 
     case 0: 
      arrColumn1[counter1] = dictionary[i]; 
      ++counter1; 
      ++mainCounter; 
      break; 
     case 1: 
      arrColumn2[counter2] = dictionary[i]; 
      ++counter2; 
      ++mainCounter; 
      break; 
     case 2: 
      arrColumn3[counter3] = dictionary[i]; 
      ++counter3; 
      ++mainCounter; 
      break; 
     case 3: 
      arrColumn4[counter4] = dictionary[i]; 
      ++counter4; 
      mainCounter = 0; 
      break; 
     } 
} 

void printRecordFunction(int colToSearch, string findThis, string arr1[], string arr2[], string arr3[], string arr4[]){ 

int foundIndex=0; 


if(colToSearch == 1){ 
     for(int i = 0; i<arr1.length; ++i){ 
      if(strcmp(arr1[i], findthis)==0){ 
       foundIndex = i; 
       break; 
      } 
     } 
}else if(colToSearch == 2){ 
     for(int i = 0; i<arr2.length; ++i){ 
      if(strcmp(arr2[i], findthis)==0){ 
       foundIndex = i; 
       break; 
      } 
     } 
}else if(colToSearch == 3){ 
     for(int i = 0; i<arr3.length; ++i){ 
      if(strcmp(arr3[i], findthis)==0){ 
       foundIndex = i; 
       break; 
      } 
     } 
}else if(colToSearch == 4){ 
     for(int i = 0; i<arr4.length; ++i){ 
      if(strcmp(arr4[i], findthis)==0){ 
       foundIndex = i; 
       break; 
      } 
     } 
} 

count<<"Record: " << arr1[i] << " " << arr2[i] << " " << arr3[i] << " " << arr4[i] << endl; 

} 

對不起,這是所有相當困難的代碼,但我希望它給你一些想法和寫入沒有意義,你可以調整它

+1

這與python無關。 – Hamatti 2014-11-25 03:45:00