#-*- 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();

  

Python 字符串转JSON; 先装字典在转JSON; json.dumps(d)的更多相关文章

  1. python字符串删除,列表删除以及字典删除的总结

    一:字符串删除  1,字符串本身是不可变的,一个字符串定义以后,对他本身是不能做任何操作的,所以的增删改都是对原字符串拷贝的副本的操作,原来的字符串还是原来的字符串,它本身并没 有变 2,字符串本身是 ...

  2. json数据处理:读取文件中的json字符串,转为python字典

    方法1: 读取文件中的json字符串, 再用json.loads转为python字典 import json str_file = './960x540/config.json' with open( ...

  3. python字符串/列表/字典互相转换

    python字符串/列表/字典互相转换 目录 字符串与列表 字符串与字典 列表与字典 字符串与列表 字符串转列表 1.整体转换 str1 = 'hello world' print(str1.spli ...

  4. json模块:字符串与字典之间的转换--loads,dumps,load,dump

    一.json转化成字典: product.json文件:将该文件内容转换成python中字典,方法如下: 方法一:使用.loads(),先读后转换 import  json   #导入json, 注: ...

  5. Python txt文件读取写入字典的方法(json、eval)

    link:https://blog.csdn.net/li532331251/article/details/78203438 一.使用json转换方法 1.字典写入txt import json d ...

  6. python字符串、列表、字典的常用方法

    一.python字符串的处理方法 >>> str = ' linzhong LongXIA ' >>> str.upper() #字符串str全部大写 ' LINZ ...

  7. python字符串、列表和字典的说明

    python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...

  8. Python字符串转为字典方法大全

    方法一: 通过内置函数eval str_info = '{"name": "test", "age": 18}' dict_info = e ...

  9. python字符串与字典转换

    经常会遇到字典样式字符串的处理,这里做一下记录. load load针对的是文件,即将文件内的json内容转换为dict import json test_json = json.load(open( ...

  10. python字符串/列表/元组/字典之间的相互转换(5)

    一.字符串str与列表list 1.字符串转列表 字符串转为列表list,可以使用str.split()方法,split方法是在字符串中对指定字符进行切片,并返回一个列表,示例代码如下: # !usr ...

随机推荐

  1. DBProxy

    DBProxy/USER_GUIDE.md at master · Meituan-Dianping/DBProxy   https://github.com/Meituan-Dianping/DBP ...

  2. 用十条命令在一分钟内检查 Linux 服务器性能

    原文地址: http://www.oschina.net/news/69132/linux-performance 如果你的Linux服务器突然负载暴增,告警短信快发爆你的手机,如何在最短时间内找出L ...

  3. Android 获得手机屏幕真实的宽高

    http://stackoverflow.com/questions/1016896/get-screen-dimensions-in-pixels WindowManager w = activit ...

  4. mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'报错处理

    年后回来查看mysql运行状况与备份情况,登录mysql从库查看主从同步状态 mysql> show slave status\G; *************************** . ...

  5. 6、Python变量

    Python变量 变量的定义 变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变. 变量的命名 变量名有字母.数字.下划线组成. 数字不能开头 不可以使用关键字 a a1 a_ ...

  6. angular default project (angular.json的解读)

    Change the default Angular project Understanding it's purpose and limits Klaus KazlauskasFollow Nov ...

  7. centos清除dns cache.

    # /etc/init.d/nscd restart # service nscd restart # service nscd reload # nscd -i hosts https://www. ...

  8. How to make an IntelliJ IDEA plugin in less than 30 minutes

    Sometimes it is a nice thing to extend an editor to have it do some new stuff, like being able to re ...

  9. 〖Java〗Eclispe安装和使用viplugin

    习惯了VIM的操作,每次打开Eclipse都习惯性的按下 hjkl: 感觉蛋疼了使用一下VIPlugin,发现给编码速度造成了成吨的伤害- 这个插件对于习惯于使用VIM的程序员来说,简直太有必要了.. ...

  10. JavaScript绑定this

    问题描述 var a = { one: 1, haha() { console.log(this.one) } } setTimeout(a.haha, 1000) 在上例中,函数haha引用了thi ...