Python:在两个文件中查找字符串并打印所有行

2024-06-26 17:40:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从另一个数据库的文件中查找发生的情况

我的文件如下:

文件1:群集名称

文件2:时间戳、群集名称、日志

我想检查第一个文件中的集群是否在第二个文件中,并打印所有行

例如:

文件1:

  • clusterA
  • 集群b
  • clusterC

文件2:

  • 2019年,clusterB,日志
  • 2020年,clusterC,日志
  • 2017年,clusterZ,日志

输出应该是这样的

Input: clusterB, clusterZ
output: 2017, clusterZ, log
        2019, clusterB, log
import pandas as pd

#ARRAY
my_value = []
cluster_value = []

#READ THE FILES
my_data_file = pd.read_csv('my_data.txt', sep=',')
log_file = pd.read_csv('log.txt', sep=',')

#TAKE THE COLUMN WITH THE CLUSTERS
for row in my_data_file[my_data_file.columns[1]]:
    my_value.append(row)

for row in log_file[log_file.columns[0]]:
    cluster_value.append(row)

#Restult
print("_______________")
print(list(set(my_value) & set(cluster_value)))
print("_______________")

它可以工作,但我需要打印所有日志。我不知道如何将我的操作结果与打印我需要的内容联系起来


Tags: 文件the名称logdatavaluemyfile
1条回答
网友
1楼 · 发布于 2024-06-26 17:40:02

使用正则表达式

  • 不需要熊猫来读取这个简单的文件

代码

import re

def search(key_file, search_file):
    with open(key_file) as kfile:
      keys = '|'.join(line.rstrip().split(',')[0] for line in kfile.readlines())
    # regex for cluster names
    regex = re.compile(keys)

    with open(search_file) as search_data:
      for line in search_data:
        if regex.search(line):
          print(line.rstrip())

search('mydata.txt', 'log.txt')

输入

'mydata.txt'(注','无所谓,即忽略)

clusterB,
clusterZ

'log.txt'

2019, clusterB, log
2020, clusterC, log
2017, clusterZ, log

输出

2019, clusterB, log
2017, clusterZ, log

相关问题 更多 >