[英]Compare two text file using python
我正在尝试比较两个文件,并在第一个文件中提取与第一列的第二个文件相对应的行。 例如:
文件1:
VarID GeneID TaxName PfamName
3810359 1327 Isochrysidaceae Methyltransf_21&Methyltransf_22
6557609 5442 Peridiniales NULL
4723299 7370 Prorocentrum PEPCK_ATP
3019317 10454 Dinophyceae NULL
2821675 10965 Bacillariophyta PK;PK_C
5559318 12824 Dinophyceae Cyt-b5&FA_desaturase
档案2:
VarID
3810359
6557609
4723299
5893435
4852156
对于输出,我想要这个文件:
VarID GeneID TaxName PfamName
3810359 1327 Isochrysidaceae Methyltransf_21&Methyltransf_22
6557609 5442 Peridiniales NULL
4723299 7370 Prorocentrum PEPCK_ATP
我尝试了这段代码:
f1 = sys.argv[1]
f2 = sys.argv[2]
file1_rows = []
with open(f1, 'r') as file1:
for row in file1:
file1_rows.append(row.split())
# Read data from the second file
file2_rows = []
with open(f2, 'r') as file2:
for row in file2:
file2_rows.append(row.split())
# Compare data and compute results
results = []
for row in file2_rows:
if row[:1] in file1_rows:
results.append(row[:4])
else:
results.append(row[:4])
# Print the results
for row in results:
print(' '.join(row))
你能帮我么 ??? 谢谢 !!
您的问题在这里:
if row[:1] in file1_rows:
row[:1]
返回具有1个字段的列表(该行的第一列)。 而是直接搜索该行。
这是新的代码:
if row[0] in file1_rows:
另外,如果(我想这是在调试中错误添加的二重奏),则删除与此相关的else。
您还可以执行其他一些更好的实践,我在这里都写下了它们:
f1 = sys.argv[1]
f2 = sys.argv[2]
with open(f1, 'r') as file1:
file1_rows = file1.read().splitlines()
# Read data from the second file
with open(f2, 'r') as file2:
file2_rows = file2.read().splitlines()
# Compare data and compute results
results = []
for row2 in file2_rows:
for row in file1_rows:
if row2 in row:
results.append(row)
break
print('\n'.join(results))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.