写在最前面,改json文件是多个json组成,每个json最前面都有一个名称,需要先获取名称之间的json串,然后再去json串之中的某个固定值的最大值
直接上代码:
#!/usr/bin/env python
import re
import json
def get_node_num():
node_name=[]
with open('test.log','r+') as file:
resultlines=file.readlines()
for resultline in resultlines:
if resultline.startswith('CHN-'):
node_name.append(resultline.strip())
return node_name
def get_result():
node_name_list=get_node_num()
f = open('文件名','r')
buff = f.read()
for num in range(len(node_name_list)):
if num <= '430':
start_mark=node_name_list[num]
end_mark=node_name_list[num+1]
elif num == '431':
start_mark=node_name_list[num]
end_mark=''
pat = re.compile(start_mark+'(.*?)'+end_mark,re.S)
result = pat.findall(buff)
result = re.sub('[==.*==]','',result[0])
value_list=[]
with open('test1.json','w+') as file_w:
file_w.write(result)
with open('test1.json','r+') as file_r:
r_lines=file_r.readlines()
for line in r_lines:
if 'value' in line:
value_list.append(line.split()[1])
try:
print node_name_list[num]+","+max(value_list)
except Exception as e:
print node_name_list[num],value_list
f.close()
if __name__ == '__main__':
get_result()
网友评论