[英]comparing two tab seperated files for multiple column and combine matches
I need to compare two files for first two columns and select the matched ones with additional information of second file like;我需要比较前两列的两个文件,并选择具有第二个文件附加信息的匹配文件,例如;
input1:输入1:
0 1
2 4
5 6
input2:输入2:
2 4 xyz
5 4 asv
0 1 qwe
output is输出是
2 4 xyz
0 1 qwe
i tried this at python but does not work properly;我在 python 上试过这个,但不能正常工作; like if there are more 1 at first column it cannot give proper output就像如果第一列有更多的 1,它就无法提供正确的输出
#!/usr/bin/python
import sys
f1 = 'file1.txt'
f2 = 'file2.txt'
if len(sys.argv) == 3:
f1 = sys.argv[1]
f2 = sys.argv[2]
with open(f1) as file_1, open('out.txt', 'w') as of:
for l1 in file_1:
col_of_f1 = int(l1.split()[0])
with open(f2) as file_2:
for l2 in file_2:
col_of_f2 = l2.split()
if len(col_of_f2) < 1:
break
col_of_f2 = int(col_of_f2[0])
if col_of_f1 == col_of_f2:
of.write(l2)
break
Replace with this code after initializing f1 and f2初始化 f1 和 f2 后用此代码替换
s1=open(f1,'r').read().split('\n')
s2=open(f2,'r').readlines()
of=open('out.txt','w')
ans=''
for i in s1:
for j in s2:
if i in j:
ans+=j+'\n'
of.write(ans)
of.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.