Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python链式区间比较_Python_Csv_Comparison_Intervals - Fatal编程技术网

Python链式区间比较

Python链式区间比较,python,csv,comparison,intervals,Python,Csv,Comparison,Intervals,我尝试在两个文件之间进行链式比较,如果结果在指定的时间间隔内,则打印/写出结果 这就是我目前所拥有的 test1文件: A0AUZ9,7,17 #just this one line 测试2文件: A0AUZ8, DOC_PP1_RVXF_1, 8, 16, PF00149, O24930 A0AUZ9, LIG_BRCT_BRCA1_2, 127, 134, PF00533, O25336 A0AUZ9, LIG_BRCT_BRCA1_1, 127, 132, PF00533, O25336

我尝试在两个文件之间进行链式比较,如果结果在指定的时间间隔内,则打印/写出结果

这就是我目前所拥有的

test1文件:

A0AUZ9,7,17 #just this one line
测试2文件:

A0AUZ8, DOC_PP1_RVXF_1, 8, 16, PF00149, O24930
A0AUZ9, LIG_BRCT_BRCA1_2, 127, 134, PF00533, O25336
A0AUZ9, LIG_BRCT_BRCA1_1, 127, 132, PF00533, O25336
A0AUZ9, DOC_PP1_RVXF_1, 8, 16, PF00149, O25685
A0AUZ9, DOC_PP1_RVXF_1, 8, 16, PF00149, O25155
还有剧本本身:

results = []

with open('test1', 'r') as disorder:
    for lines in disorder:
        cells = lines.strip().split(',')
        with open('test2', 'r') as helpy:
            for lines in helpy:
                blocks = lines.strip().split(',')
                if blocks[0] != cells[0]:
                    continue
                elif cells[1] <= blocks[2] and blocks[3] <= cells[2]:
                    results.append(blocks)                    

with open('test3','wt') as outfile:
    for i in results:
        outfile.write("%s\n" % i)
results=[]
以open('test1','r')作为无序:
对于混乱的线路:
单元格=行.strip().split(',')
使用open('test2','r')作为帮助:
对于helpy中的行:
blocks=lines.strip().split(','))
如果块[0]!=单元格[0]:
持续

elif单元格[1]它不能按预期工作的原因之一是您正在比较字符串而不是数字

然而,可能有更好的方法来做你想做的事情。假设第一个文件足够小,可以放入内存:

import csv
from collections import defaultdict

lookup_table = defaultdict(list)

with open('test1.txt') as f:
   reader = csv.reader(f)
   for row in reader:
      lookup_table[row[0]].append((int(row[1]),int(row[2])))

with open('test2.txt') as a, open('results.txt', 'w') as b:
   reader = csv.reader(a)
   writer = csv.writer(b)

   for row in reader:
      record = lookup_table.get(row[0])
      if record:
         if record[0] <= int(row[2]) and record[1] <= int(row[3]):
             writer.writerow(row)
导入csv
从集合导入defaultdict
lookup\u table=defaultdict(列表)
将open('test1.txt')作为f:
读卡器=csv。读卡器(f)
对于读取器中的行:
查找表[第[0]行]。追加((第[1]行的int),第[2]行的int)
将open('test2.txt')作为a,将open('results.txt','w')作为b:
读卡器=csv。读卡器(a)
writer=csv.writer(b)
对于读取器中的行:
record=lookup\u table.get(第[0]行)
如果记录:

如果记录[0],文件是否已排序?是的,根据第一列中ID的名称(字母顺序),此处的空格将出现问题。strip()只删除前导空格和尾随空格,而不删除字符串中的空格。只要可以完全忽略空格,替换(“,”)可能会有所帮助。哦,天哪,删除空格实际上解决了问题!非常感谢你!非常感谢。根据上面的建议删除空间后,我得到了一些结果,但在深入研究之后,其中一些是错误的。将它们指定为整数就成功了。再次感谢!