# !/user/bin/python
# -*- coding: utf-8 -*-
"""
"""
import re
import json
from xlog import xwrite
from collections import defaultdict
def defa(prot_h, add_ip):
dic = defaultdict(list)
for key, value in zip(prot_h, add_ip):
dic[key].append(value)
str_json = json.dumps(dic, indent=2, ensure_ascii=False)
print(str_json.replace("[", "").replace(‘]‘, ‘‘).replace(‘ ‘, ‘‘))
def listToJson(keys, lst):
list_json = dict(zip(keys, lst))
str_json = json.dumps(list_json, indent=2) # 将python对象编码成Json字符串
return str_json
def each_line(path):
while True:
arr_str = []
arr_num = []
file_path = path
# p1 = re.compile(r‘(.*?)-mlh-INFO-index:(.*),gps(.*)-(.*)‘)
p1 = re.compile(r‘(.*?)-mlh-INFO-(.*)-(.*)‘)
f = open(file_path, ‘r‘)
for each_line in f:
# xwrite.normal(each_line)
if each_line.strip():
res1 = each_line.replace(‘ ‘, ‘‘)
res2 = p1.search(res1)
str1 = res2.group(2).split(",", -1)
for i in str1:
a = i.split(":", -1)
xwrite.normal(a)
if len(a) >= 2:
arr_str.append(a[0])
arr_num.append(float(a[1]))
xwrite.normal(arr_str)
xwrite.normal(arr_num)
defa(arr_str, arr_num)
return listToJson(arr_str, arr_num)
if __name__ == ‘__main__‘:
print (each_line("/Users/v_qinyuan/Documents/python.project/shop/shuju.txt"))
xwrite.normal(each_line("/Users/v_qinyuan/Documents/python.project/shop/shuju.txt"))
瞎写随笔2---还是各种取值和解析