开发者社区> 问答> 正文

python高效查找2个文件相同字段并输出相应行该怎么做?

python高效查找2个文件相同字段并输出相应行该怎么做?

展开
收起
游客ahv54x37wvm7u 2021-10-31 16:14:10 438 0
1 条回答
写回答
取消 提交回答
  • fd = {}

    for l in open('a.txt', 'r'): ar = l.split(' ') if len(ar) == 2: fd[ar[0]] = ar[1]

    with open('out.txt', 'w') as fw: for l in open('b.txt', 'r'): ar = l.split(' ') if len(ar) == 2: if ar[0] in fd: fw.write(' '.join([ ar[0],fd[ar[0]].strip('\n'),ar[0],ar[1]]))

    print('ok') 你试试吧 性能应当还可以,百万的数据量的话不会超过一分钟

    2021-10-31 16:14:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载