如何比较2个文件并生成匹配列表,获取值错误?

选择 | 换行 | 行号
  1.  
  2. f = open("Desktop/MEME_IDS", "r")  #open file with list of terms to search for
  3. patterns = {}  #create an open dictionary (this is a string)
  4. while True:
  5.     line = f.readline()  #read each line of file1, put into variable line
  6.     if (line == ''):    #at the end of file, stop
  7.         break
  8.     patterns[line] = None  #put each line into a list with the key patterns
  9.  
  10. f = open("Desktop/genelistAFUA.txt", "r")  #open next file
  11. while True:
  12.     line = f.readline()  #read each line in, with break for end of file, as above
  13.     if (line == ''):
  14.         break
  15.     col1, col2 = line.split("\t")  #split the file into 2 columns, that are separated by a tab, what I want to match is in col1, info i need is in col2
  16.     if (col1 in patterns):   # if the value in col1 matches the patterns list above, then print col2
  17.         print col2
  18.  
  19.  

#从A到从全基因组列表解析我感兴趣的基因的子列表
#FORMAT OF FILE 1是一个简单的单值列表,第二个文件有2列,我正在匹配来自文件1的列表中的列1。
#我的错误是:
回溯(最近一次呼叫):
文件"Desktop/parsefile.py",第14行,位于
Col1,col2=line.plit("\t")#将文件拆分为两列,用制表符分隔
ValueError:需要多个值才能解包
有什么想法吗?

# 回答1


请在发布代码时使用代码标签。
不是进入While循环,而是在for循环中迭代文件对象。示例:

选择 | 换行 | 行号
  1. f = open("some_file")
  2. for line in f:
  3.     print line

当到达文件结尾时,迭代将停止。
回到你的问题上来。检查最后一个值的内容
线

# 回答2


谢谢你的帮助。如果我从Gigantor输入文件中取几行,开始、中间或结尾,程序就可以运行,所以我计划只需将文件切碎,然后从那里开始。
我本来有你建议的程序,但它不起作用。现在意识到这可能是从Get Go开始的愚蠢的输入文件。
还有,不知道怎么写代码标签,是我的错。
# 回答3


很高兴能帮上忙。许多第一次的海报都没有代码标签。那些永远不会收到的海报才是令人讨厌的!
# 回答4


谢谢,我把[代码]错认为需要输入[python]:)
另外,另一位朋友建议使用strip(我认为这类似于我在Perl中使用的chomp),这完全解决了这个问题-我还必须保留更复杂的While=True循环,否则它就不能工作了,?

标签: python

添加新评论