Python 字符串转JSON; 先装字典在转JSON; json.dumps(d)

时间:2021-11-21 18:13:22
#-*- coding:UTF-8 -*-

import os;
import json class MysqlUtil():
def __init__(self):
pass if __name__ == '__main__': document = open("C:/Users/ald/Desktop/log/access_api/access_api.log", "r");
document_w = open("C:/Users/ald/Desktop/log/access_api/access_api.json", "w");
doc_list = document.readlines()
for line in doc_list:
line_arr=line.split('"')
#print line_arr
if(len(line_arr)<=3):
continue
ip_arr=line_arr[0].split(' ')
if(len(ip_arr)==0):
continue
ip_str=ip_arr[0] url_arr=line_arr[1].split(' ')
if(len(url_arr)<=2):
continue
url_type=url_arr[0]
url_str=url_arr[1] time_arr=line_arr[2].split(' ')
if(len(url_arr)==0):
continue
response_time=time_arr[2]
response_status=time_arr[1] d={}
d['ip']=ip_str
d['url_type']=url_type
d['url_str']=url_str
d['response_time']=response_time
d['response_status']=response_status
JsonStr = json.dumps(d)
document_w.write( JsonStr+"\n")
count=count+1
print "运行成功",count
document.close();
document_w.close();