Python:匹配两个文件中的两列?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (595)

我想创建一个Python脚本,它与输入文件的前两列匹配(文件1)

10000D 10000R
10003D 10003R

并在保存数据集的另一个输入文件(文件2)中将这两列匹配到第2列和第4列。

0 10000D 0 10000R 0.05
0 10001D 0 10001D 0.06
0 10003D 0 10003R 0.09

一旦这些列匹配,我想打印出与文件2匹配的文件1中的列保存在新的输出文件中的行。输出文件应该如下所示:

0 10000D 0 10000R 0.05
0 10003D 0 10003R 0.09

我的代码如下所示:

#Python code for pi-hats extraction

#!/usr/bin/python

#open and read file to read from (F1), file to match to (F2), File to write and save to (F3)

F1 = open("File_1", "r") #File_1 is original file, has 2 columns
F2 = open("File_2", "r") #where dataset is kept
F3 = open("File_3", "w") #where matches are stored

for match1 in sorted(F1):
    if match1 in F2:
        F3.write(match)
        F3.close()
exit

但是,当我运行这个代码时,我没有得到任何匹配。 有什么建议么?

提问于
用户回答回答于
import csv

with open("File_1", "r") as F1:  #File_1 is original file, has 2 columns
    # split the file using a space as delimiter and read it to the memory:
    F1_d = sorted(csv.reader(F1, delimiter=' ')) 

with open("File_2", "r") as F2:  #where dataset is kept
    # split the file using space again, and read it to a dictionary
    # structure indexed by second and forth columns:
    F2_d = {(row[1], row[3]): row for row in csv.reader(F2, delimiter=' ')}

with open("File_3", "w") as F3: #where matches are stored
    for match1 in F1_d: 
        if tuple(match1) in F2_d: # search for a match using the index defined
            F3.write(' '.join(F2_d[match1]) + '\n')

扫码关注云+社区

领取腾讯云代金券