【问题标题】:Structure Android LogCat Text File to Structured Pandas DF将 Android LogCat 文本文件结构化为结构化 Pandas DF
【发布时间】:2021-10-17 02:45:31
【问题描述】:

我想将 LogCat 文本文件的行转换为结构化的 Pandas DF。我似乎无法正确地概念化我将如何做到这一点......这是我的基本伪代码:

dateTime = []
processID = []
threadID = []
priority = []
application = []
tag = []
text = []

logFile = "xxxxxx.log"

for line in logfile:
     split the string according to the basic structure
     dateTime = [0]
     processID = [1]
     threadID = [2]
     priority = [3]
     application = [4]
     tag = [5]
     text = [6]
     append each to the empty list above

write the lists to pandas dataframe & add column names

问题是:我不知道如何用这种结构正确定义分隔符

08-01 14:28:35.947 1320 1320 D wpa_xxxx: wlan1:skip--ssid

【问题讨论】:

  • 你能指定08-01 14:28:35.947 1320 1320 D wpa_xxxx: wlan1: skip--ssid的七个部分是什么吗?

标签: python android pandas logging adb


【解决方案1】:
import re
import pandas as pd

ROW_PATTERN = re.compile(r"""(\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}\.\d+) (\d+) (\d+) ([A-Z]) (\S+) (\S+) (\S+)""")

with open(logFile) as f:
    s = pd.Series(f.readlines())

df = s.extract(ROW_PATTERN)
df.columns = ['dateTime', 'processID', 'threadID', 'priority', 'application', 'tag', 'text']

这会将logFile的每一行读入Series中的一行,然后可以通过正则表达式中的每个组将其扩展为DataFrame。这假定08-01 14:28:35.947 是每行中的第一个值,后续值用空格分隔。

【讨论】:

  • 所以,我没有逐字使用你的代码,但它确实引发了一些想法。这是代码:logs = open(r"C:\Users\rzw3ch\Documents\vehicle_logs\2021_tahoe_white_i3mh_no_comm_8_09_2021\gmlogger\gmlogger_2021_8_9_8_55_53\main.log",encoding='ISO-8859-1') logsDf = pd.DataFrame()对于日志中的行: logsDf = logsDf.append(pd.Series(pd.Series(line.split(' ')).values.reshape(-1)),ignore_index=True) 问题是,这种方法非常缓慢。其中一个文件有 150 万行日志。所以,现在我需要找到一种方法来分块或多线程阅读......有什么想法吗?
  • 哪个更慢:f.readlines()s.extract(ROW_PATTERN)?
猜你喜欢
  • 2011-08-18
  • 2017-10-26
  • 2011-03-10
  • 2023-03-16
  • 2015-01-05
  • 1970-01-01
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多