JSON数据格式的转换
什么是json
- JSON是一种轻量级的数据交互格式。可以按照JSON指定的格式去组织和封装数据
- JSON本质上是一个带有特定格式的字符串
- 主要功能:json就是一种在各个编程语言中流通的数据格式,负责不同编程语言中的数据传递和交互。类似于:
- 国际通用语言-英语
- 中国56个民族不同地区的通用语言-普通话
json有什么用
- 各种编程语言存储数据的容器不尽相同,在Python中有字典dict这样的数据类型,而其它语言可能没有对应的字典
为了让不同的语言都能够相互通用的互相传递数据,JSON就是一种非常良好的中转数据格式。以Python的C语言互传数据为例:
1 2
| Python格式数据->Json格式数据->C语言程序接受Json格式数据并转化为C格式数据继续使用 C格式数据->Json格式数据->Python语言程序接受Json格式数据并转化为Python格式数据继续使用
|
json格式数据转化
json格式的数据要求很严格,下面我们看一下要求
1 2 3 4
| {"name":"admin","age":18}
{{"name":"admin","age":18},{"name":"root","age":16},{"name":"张三","age:20"}}
|
Python数据和Json数据的相互转化
Python数据和Json数据的相互转化
1 2 3 4 5 6 7 8
| import json
data = [{"name":"老王","age":16}.{"name":"张三","age":20}]
data = json.dump(data)
data = json.loads(data)
|
总结
json:是一种轻量级的数据交互格式,采用完全独立于编程语言的文本格式来存储和表示数据(就是字符串)
Python语言使用JSON有很大优势,因为:JSON无非就是一个单独的字典或一个内部元素都是字典的列表
所以JSON可以直接和Python的字典或列表进行无缝转换
json格式数据转化
通过json.dumps(data)方法把python数据转化为了json数据
如果有中文可以带上:ensure_ascii=False参数来确保中文正常转换
通过json.loads(data)方法把json数据转化为了python列表或字典
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| """ 演示JSON数据和Python字典的相互转换 """ import json
data = [{"name": "张大山", "age": 11}, {"name": "王大锤","age": 13}, {"name": "赵小虎", "age": 16}] json_str = json.dumps(data, ensure_ascii=False) print(type(json_str)) print(json_str)
d = {"name": "周杰轮", "addr": "台北"} json_str = json.dumps(d, ensure_ascii=False) print(type(json_str)) print(json_str)
s = '[{"name": "张大山", "age": 11}, {"name": "王大锤","age": 13}, {"name": "赵小虎", "age": 16}]' l = json.loads(s) print(type(l)) print(l)
s = '{"name": "周杰轮", "addr": "台北"}' d = json.loads(s)
|
运行结果
1 2 3 4 5 6
| <class 'str'> [{"name": "张大山", "age": 11}, {"name": "王大锤", "age": 13}, {"name": "赵小虎", "age": 16}] <class 'str'> {"name": "周杰轮", "addr": "台北"} <class 'list'> [{'name': '张大山', 'age': 11}, {'name': '王大锤', 'age': 13}, {'name': '赵小虎', 'age': 16}]
|
pyecharts模块简介
pyecharts模块
- 如果想要做出数据可视化效果图,可以借助pyecharts模块来完成
- 概况:Echarts是个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而Python是门富有表达力的语言,很适合用于数据处理,当数据分析遇上数据可视化时pyecharts诞生了
pyecharts模块安装
pyecharts的入门使用
pyecharts入门
基础折线图
1 2 3 4 5 6 7 8 9 10
| from pyecharts.charts import Line
line = Line()
line.add_xaxis(["中国", "美国", "英国"])
line.add_yaxis("GDP", [30, 20, 10])
line.render()
|
pyecharts有哪些配置选项
- pyecharts模块中有很多的配置选项,常用到2个类别的选项:
set_global_opots方法
- 这里全局配置选项可以通过set_global_opts方法来进行配置,相应的选项和选项的功能如下:
- TitleOpts:标题配置项
- LegendOpts:图例配置项
- ToolboxOpts:工具箱配置项
- VisualMapOpts:视觉映射配置项
- TooltipOpts:提示框配置项
- DataZoomOpts:区域缩放配置项
- 系列配置项,我们在后面构建案例时讲解
set_global_opts方法
1 2 3 4 5 6 7
| line.set_global_opts( title_opts=TitleOpts("测试", pos_left="center", pos_bottom="1%"), legend_opts=LegendOpts(is_show=True), toolbox_opts=ToolboxOpts(is_show=True), visualmap_opts=VisualMapOpts(is_show=True), tooltip_opts=TooltipOpts(is_show=True), )
|
总结
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| """ 演示pyecharts的基础入门 """
from pyecharts.charts import Line from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
line = Line()
line.add_xaxis(["中国", "美国", "英国"])
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()
|
练习
Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| """ 演示可视化需求1:折线图开发 """ 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()
us_data = us_data.replace("jsonp_1629344292311_69436(", "") jp_data = jp_data.replace("jsonp_1629350871167_29498(", "") in_data = in_data.replace("jsonp_1629350745930_63180(", "")
us_data = us_data[:-2] jp_data = jp_data[:-2] in_data = in_data[:-2]
us_dict = json.loads(us_data) jp_dict = json.loads(jp_data) in_dict = json.loads(in_data)
us_trend_data = us_dict['data'][0]['trend'] jp_trend_data = jp_dict['data'][0]['trend'] in_trend_data = in_dict['data'][0]['trend']
us_x_data = us_trend_data['updateDate'][:314] jp_x_data = jp_trend_data['updateDate'][:314] x_data = in_trend_data['updateDate'][:314]
us_y_data = us_trend_data['list'][0]['data'][:314] jp_y_data = jp_trend_data['list'][0]['data'][:314] in_y_data = in_trend_data['list'][0]['data'][:314]
line = Line()
line.add_xaxis(us_x_data)
line.add_yaxis("美国确诊人数", us_y_data, label_opts=LabelOpts(is_show=False)) line.add_yaxis("日本确诊人数", jp_y_data, label_opts=LabelOpts(is_show=False)) line.add_yaxis("印度确诊人数", in_y_data, label_opts=LabelOpts(is_show=False))
line.set_global_opts( title_opts=TitleOpts(title="2020年美日印三国确诊人数对比折线图", pos_left="center", pos_bottom="1%") )
line.render()
f_us.close() f_jp.close() f_in.close()
|