循环问题

我正在编写一个程序来比较两个不同的文件。对于文件1中的每一行,我想将其与文件2中的所有行进行比较,然后转到文件1中下一行。下面的代码不会循环到文件1的下一行,有人能给我一些建议吗?

选择 | 换行 | 行号
  1. #! /usr/bin/env python
  2.  
  3. import sys
  4. import fileinput
  5.  
  6. # Open the two files
  7. f1 = open(sys.argv[1], "r")
  8. f2 = open(sys.argv[2], "r")
  9. for line in f1:
  10.     chrR,chrStart,chrEnd,name,score,strand1,codingStart,codingEnd,itemRbg,blockCount,blockSize,BlockStart = line.strip().split()
  11.     chr = range(int(chrStart), int(chrEnd))    
  12.     lncRNA = set(chr)
  13.     for line in f2:
  14.         chrC,clustStart,clustEnd,annote,score,strand = line.strip().split()
  15.         clust = range(int(clustStart), int(clustEnd))
  16.         cluster = set(clust)
  17.         if strand1 == '-':
  18.             if chrR == chrC:
  19.                 if strand1 == strand:
  20.                     if lncRNA & cluster:
  21.                         print name, annote, 'transcript'
  22.                     else:
  23.                         continue
  24.                 continue
  25.         break
# 回答1

将文件对象重置为开头f1.寻道(0)-或-使用file方法将第一个文件读入列表readlines()并在列表上迭代。

标签: python

添加新评论