擅长:python、mysql、java
<p>这里有一个正则表达式方法:</p>
<p>步骤:</p>
<p>1-将时间和八位字节读入两个列表</p>
<p>2-遍历时间并存储“0”元素的最后一个索引,同时检查同一索引中的八位字节,确保它是否也是“0”,如果不是则抛出异常</p>
<p>3-将从“0”的结尾到最后一个索引的值相加(以八位字节为单位)</p>
<pre><code>import re
log = open('log.txt').read()
times = re.findall('Acct-Session-Time\s*=\s*(\d+)\s*', log)
octets = re.findall('Acct-Input-Octets\s*=\s*(\d+)\s*', log)
last_zero_index = 0
for i in range(0, len(times)):
if times[i] == '0':
last_zero_index = i
if octets[i] != '0':
raise Exception('Session time is reset but the usage is not')
totals = 0
for value in octets[-last_zero_index:]:
totals += int(value)
print(totals)
</code></pre>