【问题标题】:load access log file into dataframe将访问日志文件加载到数据框中
【发布时间】:2013-03-07 09:35:44
【问题描述】:

我需要处理访问日志文件并进行处理。 是否可以将访问日志之类的日志文件加载到数据框中并进行处理。 我有一个我想要处理的时间戳、响应时间和请求 url。

示例日志行:

128.0.0.2 xml12.jantzens.dk - - [04/Mar/2013:07:59:29 +0100] 15625 "POST /servlet/XMLHandler HTTP/1.1" 200 516 "-" "dk.product.xml.client.transports.ServletBridge" "-"

更新: 我正在使用常规 exp 提取响应时间和请求。 所以我试图通过添加DF来创建一个数据集。

df2 = pd.DataFrame({ 'time' : pd.Timestamp(timestamp),
                     'reponsetime' : responsetime,
                     'requesturl' : requesturl })

【问题讨论】:

  • 请为输入样本提供请求的输出。
  • 应该是可以的。如果您至少想出并尝试自己的方法来解析文件并将所需字段放入数据框中,那么您很可能会在 SO 上得到更好的响应。在那个阶段,发布相关代码并描述你面临的问题(如果你不能让它工作)。换句话说,准备好回答"What have you tried?"
  • 我已尝试查看文档,但无法找到方法。
  • 针对更新后的问题:如果您能够从日志文件中的行中提取必要的组件,有几种方法可以将它们放入DataFrame。例如,您可以创建一个字典列表,其中包含您在上面的代码中传递给DataFrame 的元素。之后,您可以将整个列表转换为DataFrame,如here 所述。同一页面上的其他示例显示了其他方法。

标签: python python-2.7 pandas


【解决方案1】:

我建议使用正则表达式并将数据加载到某种类型的内存结构中(我假设这就是您所说的数据框)。

我喜欢使用 Kodos 开发正则表达式:http://kodos.sourceforge.net/

对于你上面提供的log sn -p,下面的正则表达式会隔离一些重要的部分:

^(?P<host>[0-9.a-zA-Z ]+)\s-\s-\s\[(?P<day>[0-9]{2})/(?P<month>[a-zA-Z]{3})/(?P<timestamp>[0-9:]+ \+[0-9]{4})]\s+[0-9]+\s+"([a-zA-Z0-9 /.']+)"\s+([0-9]{3})\s+([0-9]{3})\s+"([a-zA-Z0-9 /.-]+)"\s+"([a-zA-Z0-9 /.-]+)"\s+"([a-zA-Z0-9 /.-]+)"

Kodos 也创建了一些有用的代码 sn-ps:

rawstr = r"""^(?P<host>[0-9.a-zA-Z ]+)\s-\s-\s\[(?P<day>[0-9]{2})/(?P<month>[a-zA-Z]{3})/(?P<timestamp>[0-9:]+ \+[0-9]{4})]\s+[0-9]+\s+"([a-zA-Z0-9 /.']+)"\s+([0-9]{3})\s+([0-9]{3})\s+"([a-zA-Z0-9 /.-]+)"\s+"([a-zA-Z0-9 /.-]+)"\s+"([a-zA-Z0-9 /.-]+)""""
embedded_rawstr = r"""^(?P<host>[0-9.a-zA-Z ]+)\s-\s-\s\[(?P<day>[0-9]{2})/(?P<month>[a-zA-Z]{3})/(?P<timestamp>[0-9:]+ \+[0-9]{4})]\s+[0-9]+\s+"([a-zA-Z0-9 /.']+)"\s+([0-9]{3})\s+([0-9]{3})\s+"([a-zA-Z0-9 /.-]+)"\s+"([a-zA-Z0-9 /.-]+)"\s+"([a-zA-Z0-9 /.-]+)""""
matchstr = """128.0.0.2 xml12.jantzens.dk - - [04/Mar/2013:07:59:29 +0100] 15625 "POST /servlet/XMLHandler HTTP/1.1" 200 516 "-" "dk.product.xml.client.transports.ServletBridge" "-""""

# method 1: using a compile object
compile_obj = re.compile(rawstr)
match_obj = compile_obj.search(matchstr)

# method 2: using search function (w/ external flags)
match_obj = re.search(rawstr, matchstr)

# method 3: using search function (w/ embedded flags)
match_obj = re.search(embedded_rawstr, matchstr)

# Retrieve group(s) from match_obj
all_groups = match_obj.groups()

# Retrieve group(s) by index
group_1 = match_obj.group(1)
group_2 = match_obj.group(2)
group_3 = match_obj.group(3)
group_4 = match_obj.group(4)
group_5 = match_obj.group(5)
group_6 = match_obj.group(6)
group_7 = match_obj.group(7)
group_8 = match_obj.group(8)
group_9 = match_obj.group(9)
group_10 = match_obj.group(10)

# Retrieve group(s) by name
host = match_obj.group('host')
day = match_obj.group('day')
month = match_obj.group('month')
timestamp = match_obj.group('timestamp')

您可以很容易地在此基础上将日志加载到内存中并开始处理。

【讨论】:

猜你喜欢
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多