2014-11-25 68 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