【发布时间】:2018-03-01 09:47:42
【问题描述】:
我正在尝试在两个文件之间进行链式比较,如果结果在指定的时间间隔内,则打印/写出结果。
这是我目前所拥有的。
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)
我的首选输出是只有 test3 中的行,即:
在第一列有匹配的 id
第 3 列和第 4 列中的两个数值介于 test1 文件中给出的值之间
我没有得到任何输出,我不确定哪里出错了。
【问题讨论】:
-
文件排序了吗?
-
是的,按第一列中的 ID 名称(按字母顺序)
-
您将在此处遇到空格问题。 strip() 只删除前导和尾随空格,而不是字符串中的空格。只要可以完全忽略空格,replace(" ", "") 可能会有所帮助。
-
哦,天哪,删除空格实际上解决了问题!非常感谢!
标签: python csv comparison intervals