用python3对json内容逐层进行解析,拿中国天气网的接口返回数据测试,
代码如下:
# -*- coding: utf-8 -*-
import operator as op
from collections import defaultdict
class Json(object):
def __init__(self, json: str):
sth = eval(json)
load = lambda sth: sth if op.eq(type(sth).__name__, dict.__name__) else None
self.json_dict = load(sth)
self.ret_j = defaultdict(dict)
self.analyze(self.json_dict)
def analyze(self, j_dict: dict, lvl=0) -> None:
lvl += 1
for k in j_dict:
v = j_dict[k]
v_type = type(v)
try:
self.ret_j[lvl][str(j_dict)].append(f"{k}:{v}")
except:
self.ret_j[lvl][str(j_dict)] = []
self.ret_j[lvl][str(j_dict)].append(f"{k}:{v}")
if op.eq(v_type.__name__, dict.__name__):
self.analyze(v, lvl)
elif op.eq(v_type.__name__, list.__name__):
for each in v:
if op.eq(type(each).__name__, dict.__name__):
self.analyze(each, lvl)
def get_analysis(self) -> None:
print(f"这个json拢共分{len(self.ret_j)}层")
print("------")
for lvl in self.ret_j:
print(f"第{lvl}层解析")
for root in self.ret_j[lvl]:
print(f"解析内容:{root}")
for each in self.ret_j[lvl][root]:
print(each)
print("------")
if __name__ == '__main__':
try:
import requests
except:
exit(0)
url = "http://forecast.weather.com.cn/napi/h5map/city/101/jQuery1533133004035?callback=jQuery1533133004035"
r = requests.get(url)
d_r = r.content.decode()
json_4_test = d_r[d_r.index("(") + 1:d_r.index(")")]
Json(json_4_test).get_analysis()```
<p>其中json_4_test是待解析的json字符串。</p>
<p>设计思路:</p>
<p><img alt="" src="https://img.94e.cn/media/uploads/full/20200710/20200511091918.jpg"></p>
<p>补充知识:python之logging模块:将不同的日志写入到不同的文件</p>
<p>如下所示:</p>
```python
import logging.config
from logging import LogRecord
# 通常用于Linux系统下,使控制台输出的日志带颜色
class ColorFormatter(logging.Formatter):
log_colors = {
'CRITICAL': '\033[0;31m',
'ERROR': '\033[0;33m',
'WARNING': '\033[0;35m',
'INFO': '\033[0;32m',
'DEBUG': '\033[0;00m',
}
def format(self, record: LogRecord) -> str:
s = super().format(record)
level_name = record.levelname
if level_name in self.log_colors:
return self.log_colors[level_name] + s + '\033[0m'
return s
class MyFilter400(logging.Filter):
def filter(self, record: LogRecord):
if record.msg.startswith("4"):
return True
return False
class MyFilter300(logging.Filter):
def filter(self, record: LogRecord):
if record.msg.startswith("3"):
return True
return False
LOG_LEVEL = logging.INFO
LOGGER = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'color': {
'class': '__main__.ColorFormatter', # 如果你的模块不是写在启动程序中,请将__main__更换成你模块的路径,下同
'format': '%(asctime)s [%(name)s] %(levelname)s: %(message)s'
},
'default': {
'class': 'logging.Formatter',
'format': '%(message)s'
}
},
'filters': {
'filter_400': {
'()': '__main__.MyFilter400'
},
'filter_300': {
'()': '__main__.MyFilter300'
}
},
'handlers': {
'console': {
'level': LOG_LEVEL,
'class': 'logging.StreamHandler',
'formatter': 'color',
},
'file1': {
'level': LOG_LEVEL,
'class': 'logging.FileHandler',
'mode': 'w',
'formatter': 'default',
'filename': '400_log.txt',
'encoding': 'utf-8',
'filters': ['filter_400']
},
'file2': {
'level': LOG_LEVEL,
'class': 'logging.FileHandler',
'mode': 'w',
'formatter': 'default',
'filename': '300_log.txt',
'encoding': 'utf-8',
'filters': ['filter_300']
},
},
'loggers': {
'__main__': {
'handlers': ['file1', 'file2', 'console'],
'level': LOG_LEVEL,
},
}
}
logging.config.dictConfig(LOGGER)
logger = logging.getLogger(__name__)
logger.debug('200,this is a logger debug message')
logger.info('302,this is a logger info message')
logger.warning('301,this is a logger warning message')
logger.error('404,this is a logger error message')
logger.critical('500,this is a logger critical message')
print("%s" % __name__)
运行效果图:
控制台:
文件:
3开头的写入到300_log.txt
4开头的写入到400_log.txt
特别注意,使用过滤器的一个问题
class MyFilter400And500(logging.Filter):
def filter(self, record: LogRecord):
if record.msg.startswith("4") or record.msg.startswith("5"):
return True
return False
# record.msg = "404, %s, %s"
logger.info(f"{status_code}, %s, %s", website, link)
# record.msg = "%s, %s, %s",这就导致过滤器返回False
logger.info("%s, %s, %s", status_code, website, link)
因此,如果发现消息没有写入文件,可能是消息格式的问题。
目前,官方推荐字符串格式化的方式就是第一种方式,%s和.format()的方式都不如这个好。
以上这篇Python3自定义json逐层解析器代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。
标签:requests
Powered By python教程网 鲁ICP备18013710号
python博客 - 小白学python最友好的网站!