在python中从第一个文件搜索第二个文件并输出到第三个文件
我想通过使用第一个文件作为输入并输出到第三个文件来搜索第二个文件中出现的行。它也得到了快速的名单是第二个文件超过200k,第一个超过75k在python中从第一个文件搜索第二个文件并输出到第三个文件,python,search,Python,Search,我想通过使用第一个文件作为输入并输出到第三个文件来搜索第二个文件中出现的行。它也得到了快速的名单是第二个文件超过200k,第一个超过75k **FILE 1** 1234 2324 534 235 1 643 **FILE 2** 643, 30, , , People, email@example.com,..... 1234, 45, , , People, email@example.com,..... 643, 32, , , People, email@example.com,..
**FILE 1**
1234
2324
534
235
1
643
**FILE 2**
643, 30, , , People, email@example.com,.....
1234, 45, , , People, email@example.com,.....
643, 32, , , People, email@example.com,.....
4536, 654, , , People, email@example.com,.....
898, 354, , , People, email@example.com,.....
**FILE 3**
643, 30, , , People, email@example.com,.....
1234, 45, , , People, email@example.com,.....
就是这样,伙计们。提前感谢。从第一个文件中读取ID,然后使用第二个文件的行检查起始编号是否在您收集的ID列表中。将匹配项添加到列表中,并将列表写入第三个文件
ids = []
with open('file1') as f:
ids.extend(id.strip() for id in f)
matches = []
with open('file2') as f:
for line in f:
if line.split(',')[0] in ids:
matches.append(line)
with open('file3', 'w') as f:
for match in matches:
f.write(match)
你有什么代码可以和我们分享吗?我没有任何代码可以分享。我对Python不太在行,所以在pythonSO不适合发布这样的问题。它旨在成为一个询问有关编程的特定问题的地方,而不是为您编写代码的地方。虽然Celeo已经为您发布了一个答案,但您不应该期望每次都会发生这种情况,并且应该避免提出“问题”,这些问题仅仅是对您想要的脚本或程序的一组需求的描述。它起作用了。我再次尝试了,它起作用了