提交 fb592e7e 编写于 作者: 逆流者blog's avatar 逆流者blog 🇨🇳

Python3 处理JSON格式数据(制作交易收盘价走势图)

上级 d0983bb9
from __future__ import (absolute_import, division, print_function, unicode_literals)
try:
# python2.x 版本
from urllib2 import urlopen
except:
# python3.x 版本
from urllib.request import urlopen
import requests
import json
import pygal
import math
from itertools import groupby
def draw_line(x_data, y_data, title, y_legend):
"""画折线图函数"""
xy_map = []
for x, y in groupby(sorted(zip(x_data, y_data)), key=lambda _: _[0]):
y_list = [v for _, v in y]
xy_map.append([x, sum(y_list) / len(y_list)])
x_unique, y_mean = [*zip(*xy_map)]
line_chart = pygal.Line()
line_chart.title = title
line_chart.x_labels = x_unique
line_chart.add(y_legend, y_mean)
line_chart.render_to_file(title + '.svg')
return line_chart
# 使用urllib.request包下面的urlopen下载json
json_url = 'https://codechina.csdn.net/weixin_45847167/python3-learn/-/raw/master/json/btc_close_2017.json?inline=false'
response = urlopen(json_url)
# 读取数据
req = response.read()
# 将数据写入文件
with open('btc_close_2017_urllib.json', 'wb') as f:
f.write(req)
# 加载json格式
file_urllib = json.loads(req)
print(file_urllib)
# 使用第三方模块requests 下载json
json_url = 'https://codechina.csdn.net/weixin_45847167/python3-learn/-/raw/master/json/btc_close_2017.json?inline=false'
req = requests.get(json_url)
# 将数据写入文件
with open('btc_close_2017_request.json', 'w') as f:
f.write(req.text)
file_requests = req.json()
print("file_requests = file_urllib?: {}".format(file_requests == file_urllib))
# 将数据加载到一个列表中
filename = 'btc_close_2017_urllib.json'
with open(filename) as f:
btc_data = json.load(f)
# 创建5个列表,分别存储日期和收盘价
dates = []
months = []
weeks = []
weekdays = []
closes = []
# 打印每一天的信息
for btc_dict in btc_data:
date = btc_dict['date']
month = int(btc_dict['month'])
week = int(btc_dict['week'])
weekday = btc_dict['weekday']
# python 不能将包含小数的字符串直接转成整数,如字符串6928.6492,需要先转成浮点数,再转成整数
close = int(float(btc_dict['close']))
print("{} is month {} week {}, {}, the close price is {} RMB".format(date, month, week, weekday, close))
dates.append(date)
months.append(month)
weeks.append(week)
weekdays.append(weekday)
closes.append(close)
# ----收盘价折线图(¥).svg-----
# x_label_rotation=20 让x轴上的日期标签顺时针旋转20度
# show_minor_x_labels=False 告诉图形不用显示所有的x轴标签
line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
# 标题
line_chart.title = '收盘价(¥)'
# x轴的数据
line_chart.x_labels = dates
# x轴坐标每隔20天显示一次
N = 20
# x_labels_major 让x轴每隔20天显示一次
line_chart.x_labels_major = dates[::N]
line_chart.add('收盘价', closes)
line_chart.render_to_file('收盘价折线图(¥).svg')
# x_label_rotation=20 让x轴上的日期标签顺时针旋转20度
# show_minor_x_labels=False 告诉图形不用显示所有的x轴标签
line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
# 标题
line_chart.title = '收盘价对数变换(¥)'
# x轴的数据
line_chart.x_labels = dates
# x轴坐标每隔20天显示一次
N = 20
# x_labels_major 让x轴每隔20天显示一次
line_chart.x_labels_major = dates[::N]
close_log = [math.log10(_) for _ in closes]
line_chart.add('收盘价', close_log)
line_chart.render_to_file('收盘价对数变换折线图(¥).svg')
idx_month = dates.index('2017-12-01')
line_chart_month = draw_line(months[:idx_month], closes[:idx_month], '收盘价月日均值(¥)', '月日均值')
idx_week = dates.index('2017-12-11')
line_chart_week = draw_line(weeks[1:idx_week], closes[1:idx_week], '收盘价周日均值(¥)', '周日均值')
wd = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]]
line_chart_weekday = draw_line(weekdays_int, closes[1:idx_week], '收盘价星期均值(¥)', '星期均值')
# x轴显示的星期使用中文
line_chart_weekday.x_labels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
line_chart_weekday.render_to_file('收盘价星期均值(¥).svg')
with open('收盘价Dashboard.html', 'w', encoding='utf8') as html_file:
html_file.write(
'<html><head><title>收盘价Dashboard</title><meta charset="utf-8"></head><body>\n')
html_file.write(
'<h1>收盘价Dashboard</h1>\n')
for svg in [
'收盘价折线图(¥).svg', '收盘价对数变换折线图(¥).svg', '收盘价月日均值(¥).svg',
'收盘价周日均值(¥).svg', '收盘价星期均值(¥).svg'
]:
html_file.write(
' <object type="image/svg+xml" data="{0}" height=500></object>\n'.format(svg))
html_file.write('</body></html>')
此差异已折叠。
此差异已折叠。
<html><head><title>收盘价Dashboard</title><meta charset="utf-8"></head><body>
<h1>收盘价Dashboard</h1>
<object type="image/svg+xml" data="收盘价折线图(¥).svg" height=500></object>
<object type="image/svg+xml" data="收盘价对数变换折线图(¥).svg" height=500></object>
<object type="image/svg+xml" data="收盘价月日均值(¥).svg" height=500></object>
<object type="image/svg+xml" data="收盘价周日均值(¥).svg" height=500></object>
<object type="image/svg+xml" data="收盘价星期均值(¥).svg" height=500></object>
</body></html>
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册