擅长:python、mysql、java
<p>下面是一个粗略的示例,说明如何使用<a href="https://docs.python.org/3/library/functions.html#open" rel="nofollow noreferrer">^{<cd1>}</a>打开日志文件,并使用<a href="https://docs.python.org/3/library/re.html" rel="nofollow noreferrer">^{<cd2>}</a>模块和<a href="https://docs.python.org/3/library/stdtypes.html#str.split" rel="nofollow noreferrer">^{<cd3>}</a>解析其中的一些值:</p>
<pre><code>import re
with open('myfile.log') as f:
lines = f.readlines()
data = []
for line in lines:
date = re.match(r'\d{2} \w+ \d{2}', line).group(0)
time = line.split()[3]
agent = re.search(r'agent:\s(.*?),', line).group(1)
errID = re.search(r'ErrIdText:\s(.*?),', line).group(1)
clear = re.search(r'clearedID:\s(.*?)\)', line).group(1)
row = [date, time, agent, errID, clear]
data.append(row)
for row in data:
print(row)
</code></pre>
<blockquote>
<p>Output:</p>
<pre><code>['06 May 19', '03:40:35', '12367a12', 'ERROR ID TEXT', '0x089088394']
['06 May 19', '03:44:35', '12368a15', 'Skip this item', '0x089088394']
</code></pre>
</blockquote>