gpt4 book ai didi

python - 比较两个文件

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:03 25 4
gpt4 key购买 nike

我有两个这种格式的文件

file1= filename val1 val2
file2= filename val3 val4

我想比较文件名,如果它们有相同的名字,我想得到第三个这样的文件-

filename val1 val2 val3 val4

我正在从 file1 中选择一个文件名并遍历 file2 以查看我是否得到它。然后寻找指向 file2 顶部的指针以获取下一个文件名..是否有更有效的方法?

最佳答案

听起来您正在寻找的是标准的 join 命令(不是 Python,而是标准的 Unix shell 实用程序)。这是系统手册页的片段(似乎比 Linux man page for join 更详细):

        join phonedir names

If the phonedir file contains the following names:

Adams A. 555-6235
Dickerson B. 555-1842
Erwin G. 555-1234
Jackson J. 555-0256
Lewis B. 555-3237
Norwood M. 555-5341
Smartt D. 555-1540
Wright M. 555-1234
Xandy G. 555-5015

and the names file contains these names and department numbers:

Erwin Dept. 389
Frost Dept. 217
Nicholson Dept. 311
Norwood Dept. 454
Wright Dept. 520
Xandy Dept. 999

the join command displays:

Erwin G. 555-1234 Dept. 389
Norwood M. 555-5341 Dept. 454
Wright M. 555-1234 Dept. 520
Xandy G. 555-5015 Dept. 999

关于python - 比较两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5905680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com