在Python中转换Dataframe中没有分隔符的文本?

2024-05-03 22:29:57 发布

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

所以我有很多日志txt文件,看起来有点像这样:

2021-04-01T12:54:38.156Z START RequestId: 123 Version: $LATEST

2021-04-01T12:54:42.356Z END RequestId: 123

2021-04-01T12:54:42.356Z REPORT RequestId: 123  Duration: 4194.14 ms    Billed Duration: 4195 ms    Memory Size: 2048 MB    Max Memory Used: 608 MB 

我需要使用这些数据创建一个数据框,其中每行显示一个日志,并具有以下功能:

DateTime, Keyword(start/end), RequestId, Duration, BilledDuration, MemorySize, MaxMemoryUsed

问题是每个文件都有不同的长度,并且有不同类型的日志,所以不是每一行看起来都一样,而是有模式。我从未使用过正则表达式,但我认为这是我必须使用的。那么有没有办法将这个字符串转换成数据集呢

(我的目标是执行内存使用异常检测)


Tags: 文件数据reporttxtsizeversionmblatest
2条回答

这里有一个类似的问题: Log file to Pandas Dataframe

可以将read_csv与分隔符一起使用:\s*\[

显然,我仍然不擅长在这个网站上提出正确的问题,但很高兴自己能更好地找到解决方案,所以如果其他人也有同样的问题,这就是我所做的:

import re
import gzip

counter = 0

for file in file_list:
    # open and read
    file_content = gzip.open(file, 'rb').read().decode("utf-8")
    
    # split file in lines
    splitted_file_content = file_content.splitlines()
    for line in splitted_file_content:
        # look for the report lines
        if re.search('REPORT', line):
            tokens = line.split()
    
            timestamp = tokens[0]
            id = tokens[3]
            billed_duration = tokens[9]
            max_memory_size_used = tokens[18]
            init_duration = tokens[22]
            
            # if you want to pack it in a dataframe
            df.loc[counter] = [timestamp, id, billed_duration,
                               max_memory_size_used, init_duration]
            counter += 1

相关问题 更多 >