师从黑马程序员
Json的应用
Json的概念

Json的作用

Json格式数据转化

Python数据和Json数据的相互转化

注:把字典列表变为字符串用dumps,把字符串还原回字典或列表用loads
python
import json
#准备列表,列表内每一个元素都是字典,将其转化为Json
data=[{"name":"张大仙","age":11},{"name":"王大锤","age":13},{"name":"赵小虎","age":16}]
json_str=json.dumps(data,ensure_ascii=False)
"""
如果包含中文要添加ensure_ascii=False,让内容正确的展示出来
"""
print(type(json_str))
print(json_str)
#准备字典,将字典转化为JSON
d={"name":"周杰伦","addr":"台北"}
json_str=json.dumps(d,ensure_ascii=False)
print(type(json_str))
print(json_str)
#将JSON字符串转化为Python数据类型[{k:v,k:v},{k:v,k:v}]
s='[{"name":"张大仙","age":11},{"name":"王大锤","age":13},{"name":"赵小虎","age":16}]'
l=json.loads(s)
print(type(l))
print(l)
#将JSON字符串转化为Python数据类型{k:v,k:v}
s='{"name":"周杰伦","addr":"台北"}'
d=json.loads(s)
print(type(d))
print(d)
pyechart模块介绍

pyecharts入门使用
基础折线图

python
#导包
from pyecharts.charts import Line
#创建一个折线图对象
line =Line()
#给折线图对象添加x轴的数据
line.add_xaxis(["中国","美国","英国"])
#给折线图对象添加y轴的数据
line.add_yaxis("GDP",[30,20,10])
#设置全局配置项
line.render()
pycharts的配置选项
全局配置选项
系列配置选项
set_global_opts方法


python
#导包
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts
#创建一个折线图对象
line =Line()
#给折线图对象添加x轴的数据
line.add_xaxis(["中国","美国","英国"])
#给折线图对象添加y轴的数据
line.add_yaxis("GDP",[30,20,10])
#设置全局配置项
line.set_global_opts(
title_opts=TitleOpts(title="GDP展示",pos_left="center",pos_bottom="1%"),
legend_opts=LegendOpts(is_show=True),
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(is_show=True),
)
line.render()

数据处理
python
import json
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LabelOpts
#处理数据
f_us=open("D:/美国.txt","r",encoding="UTF-8")
us_data=f_us.read()#美国的全部内容
f_jp=open("D:/日本.txt","r",encoding="UTF-8")
jp_data=f_jp.read()#日本的全部内容
f_in=open("D:/印度.txt","r",encoding="UTF-8")
in_data=f_in.read()#美国的全部内容
#去掉不合JSON规范的开头
us_data=us_data.replace("jsonp_1629344292311_69436(","")#美国
jp_data=jp_data.replace("jsonp_1629350871167_29498(","")#日本
in_data=in_data.replace("jsonp_1629350745930_63180(","")#印度
#去掉不合JSON规范的结尾
us_data=us_data[:-2]#美国
jp_data=jp_data[:-2]#日本
in_data=in_data[:-2]#印度
#JSON转Python字典
us_dict=json.loads(us_data)
jp_dict=json.loads(jp_data)
in_dict=json.loads(in_data)
#获取trand key
us_trend_data=us_dict['data'][0]['trend']
jp_trend_data=jp_dict['data'][0]['trend']
in_trend_data=in_dict['data'][0]['trend']
#获取日期数据,用于x轴,取2020年(到314结束)
us_x_data=us_trend_data['updateDate'][:314]
jp_x_data=us_trend_data['updateDate'][:314]
in_x_data=us_trend_data['updateDate'][:314]
#获取确认数据,用于y轴,取2020年(到314结束)
us_y_date=us_trend_data['list'][0]['data'][:314]
jp_y_date=jp_trend_data['list'][0]['data'][:314]
in_y_date=in_trend_data['list'][0]['data'][:314]
#生成图表
line=Line()#构建折线图对象
#添加x轴数据
line.add_xaxis(us_x_data)
#添加y轴数据
line.add_yaxis("美国确诊人数",us_y_date,label_opts=LabelOpts(is_show=False))
line.add_yaxis("日本确诊人数",jp_y_date,label_opts=LabelOpts(is_show=False))
line.add_yaxis("印度确诊人数",in_y_date,label_opts=LabelOpts(is_show=False))
#设置全局选项
line.set_global_opts(
#标题设置
title_opts=TitleOpts(title="美日印三国确诊人数对比折线图",pos_left="center",pos_bottom="1%")
)
#调用render方法,生成图表
line.render()
#关闭文件对象
f_us.close()
f_in.close()
f_jp.close()

若有侵权,请联系作者