知识点1:python读取json文件,并把内容转化为字典
import json
with open("/User/Downloads/trace.json",'r') as f:
context=f.read()
temp=json.loads(context) 注意事项:如果json文件如下格式:[{"PageJME": "1171", "GPCJME": "32234", "ServerJME": "2787", "ClientJME": "937"}, {"PageJME": "1171", "GPCJME": "32234", "ServerJME": "2787", "ClientJME": "937"}]
则可直接读取,如果是
A=[{"PageJME": "1171", "GPCJME": "32234", "ServerJME": "2787", "ClientJME": "937"}, {"PageJME": "1171", "GPCJME": "32234", "ServerJME": "2787", "ClientJME": "937"}]
则读取是要去除前面的字符 “A=”。
知识点2: python将字典存储为json格式
json.dump(temp, json_file2, ensure_ascii=False)
temp字典,json_file2文件名
知识点3:判断文件是否已经存在,若无,则新建;判断文件里是否有内容
if not os.path.exists(file_name):
os.system(r"touch %s" % file_name
if os.path.getsize(file_name):
file_name文件名
import datetime
today = datetime.date.today().strftime("%Y%m%d")将日期读取出来,存储为字符串。
如下代码主要用于,读取一个字符串代码,将其转化为字典,提出中间重要的数据。
将这个数据存储为json,(这个要实现不断读取数据,存储为json格式,这个就需要不断读取数据,添加进去,再全部存储进去,直接存储进去会出现格式问题)。然后将这个文件中的数据,取平均值,再存入另一个文件中,这个也有可能不断存储,所以也要读取出来,添加新的数据,再存储起来。
import json
import datetime
import os
def str_to_dic():
# data_jme_list=[]
data_jme = {}
with open("/Users/jiaojiaoyang/Downloads/trace.json", 'r') as f:
context=f.read()[16:]
# names=context[0:15]
# print(type(f.read()))
temp=json.loads(context)
# print(temp)
print(type(temp))
data_jme['PageJME']=temp['JMTReport'][1]['PageJME']
data_jme['GPCJME']=temp['JMTReport'][24]['GPCJME']
data_jme['ServerJME'] = temp['JMTReport'][25]['ServerJME']
data_jme['ClientJME'] = temp['JMTReport'][26]['ClientJME']
# data_jme_list.append(data_jme)
return data_jme
def save_jmt_into_file_with_data(dic,date):
file_name="jme_"+date+".json"
if not os.path.exists(file_name):
os.system(r"touch %s" % file_name)
#将文件里的文件读取出来,在数组中添加新的元素(字典)
if os.path.getsize(file_name):
json_file = open(file_name, 'r')
context = json_file.read()
temp = json.loads(context)
json_file.close()
temp.append(dic)
json_file2=open(file_name,'w', encoding='utf-8')
#temp=json.dumps(temp)
json.dump(temp, json_file2, ensure_ascii=False)
json_file.close()
return file_name
else:
with open(file_name,'w') as json_file:
data_json = []
data_json.append(dic)
#data_json = json.dumps(data_json)
json.dump(data_json, json_file, ensure_ascii=False)
print('success')
json_file.close()
return file_name
def updata_average_into_file(file_name,date):
if not os.path.exists(file_name):
os.system(r"touch %s" % file_name)
if os.path.getsize(file_name):
read_json=open(file_name,'r')
context = read_json.read()
temp = json.loads(context)
#求取平均值
average={}
sum1=0
for i in range(len(temp)):
sum1+=int(temp[i]['PageJME'])
average["PageJME"]=sum1/len(temp)
sum2=0
for i in range(len(temp)):
sum2+=int(temp[i]['GPCJME'])
average['GPCJME']=sum2/len(temp)
sum3=0
for i in range(len(temp)):
sum3+=int(temp[i]['ServerJME'])
average['ServerJME']=sum3/len(temp)
sum4=0
for i in range(len(temp)):
sum4+=int(temp[i]['ClientJME'])
average['ClientJME']=sum4/len(temp)
read_json.close()
#存储文件,将文件读取出来,再存进去
file_name2 = "jme_averge.json"
if not os.path.exists(file_name2):
os.system(r"touch %s" % file_name2)
if os.path.getsize(file_name2):
json_file = open(file_name2, 'r')
context = json_file.read()
temp = json.loads(context)
json_file.close()
temp[date]=average
json_file2=open(file_name2,'w', encoding='utf-8')
#temp=json.dump(temp)
json.dump(temp, json_file2, ensure_ascii=False)
json_file.close()
print('success')
else:
with open(file_name2,'w') as json_file:
data_json = {}
data_json[date]=average
#data_json = json.dump(data_json)
json.dump(data_json, json_file, ensure_ascii=False)
print('success')
json_file.close()
else:
print('无文件')
def save_jme_into_file(dic):
today = datetime.date.today().strftime("%Y%m%d")
file_name=save_jmt_into_file_with_data(dic, today)
updata_average_into_file(file_name,today)
if __name__ == '__main__':
today = datetime.date.today().strftime("%Y%m%d")
data1=str_to_dic()
data2=str_to_dic()
# save_jmt_into_file_with_data(data1,today)
# save_jmt_into_file_with_data(data2,today)
save_jme_into_file(data1)
# save_jme_into_file(data2)
# read_json_to_dic()


京公网安备 11010502036488号