zoukankan      html  css  js  c++  java
  • Python 字符串转JSON; 先装字典在转JSON; json.dumps(d)

    #-*- 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+"
    ")
            count=count+1
        print "运行成功",count
        document.close();
        document_w.close();
    

      

  • 相关阅读:
    ubuntu安装RabbitMQ
    pycharm中引入相对路径错误
    python之pip
    ubuntu安装django
    ubuntu16安装python3.7
    ubuntu上用源码进行一键安装mysql
    tar命令
    linux之bc命令
    linux源码安装
    ubuntu安装搜狗输入法后无法使用goland的快捷键 ctrl+alt+B
  • 原文地址:https://www.cnblogs.com/zgaspnet/p/9041920.html
Copyright © 2011-2022 走看看