1. 关于zip()与*zip()
简单例子
a
= [1,2,3]
b
= [4,5,6]
>>> print(zip(a
,b
))
<zip object at
0x000002C6761FE308>
>>> print(*(zip(a
,b
)))
(1, 4) (2, 5) (3, 6)
>>> print(*zip(*zip(a
,b
)))
(1, 2, 3) (4, 5, 6)
关于zip(): 遇到不会函数的拿简单的数字来试一下 可以发现,zip(a,b) 是将 a,b中的对应位置上的元素进行组合,组成元组,即为[(1,4),(2,5),(3,6)],但print看不到结果。需要用*zip()来显示。关于*zip(a,b): 看到 *可以理解为解压的意思,将zip(a,b)得到的[(1,4),(2,5),(3,6)]解压,会得到 (1,4) (2,5) (3,6) 。注意此输出结果中括号中间无逗号。**关于*zip(*zip(a,b)) **: 可以理解为先将(1,4) (2,5) (3,6)对应位置的元素进行组合,再利用 * 解压,组合结果为[(1,2,3),(4,5,6)], 但不输出, * 后得到(1,2,3) (4,5,6)
2. 关于math.log()
// math.log10(100)
2.0
同理
colse_log = [math.log10(_) for _ in close]
上面这条语句是算close列表中元素的10的对数的值
line_chart.x lables_major = dates[::N]
dates[::N] 的含义是取间隔为N的数作为x轴的标记点
3. 关于直接下载req.json()和读取网页内文字.get()方法
import requests
json_url
= 'http://......../文件名.json'
req
= requests
.get(json_url
)
with open (文件名
.json
,w
) as f
:
f
.write(req
.text
)
file_requests
= req
.json()
req = requests.get(json_url) :引入import模块后,利用get方法向Github服务器发送请求,并将返回结果存存储在req中。 可直接利用req.json()将文件数据转化为python列表的数据后使用。
4. 将字符串转化为数字
注意:Python不能直接将含有小数点‘2324.35867’的字符串直接转换为整数。要先将字符串转换为浮点数(float)再将浮点数转化为整数(int)
close
= [24.24434,535.57,6868.57]
int(float
['close'])
5. format()
将字符串转化为数字给某句话的不同位置赋不同的值时,可使用下面的语句,更方便一些
a
= [1,2]
b
= [3,4]
c
= [5,6]
print("{} is {} and {} ".format(a
,b
,c
))
>>> a is b and c
记:位置可用{}代替,后面用format()将对应位置的值加入即可。
6. 整体代码
'''导入json'''
import json
import requests
import math
import pygal
from itertools
import groupby
json_url
= 'http://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
req
= requests
.get(json_url
)
file_requests
= req
.json()#或用req
.json()可以直接将文件的数据转换为python列表数据
#将数据加载到一个列表中
filename
='btc_close_2017.json'
with open(filename
) as f
:
btc_data
=json
.load(f
)
#打印每一天的信息
for btc_dict
in btc_data
:
date
=btc_dict
['date']
month
=int(btc_dict
['month'])
week
=int(btc_dict
['week'])
weekday
=btc_dict
['weekday']
close
=int(float(btc_dict
['close']))
print("{} is month{}week{},{},the close price is {}RMB".format(date
,month
,week
,weekday
,close
))
filename
='btc_close_2017.json'
with open(filename
) as f
:
btc_data
=json
.load(f
)
#创建列表
dates
=[]
months
=[]
weeks
=[]
weekdays
=[]
close
=[]
#将数据添加到列表中
for btc_dict
in btc_data
:
dates
.append(btc_dict
['date'])
months
.append(int(btc_dict
['month']))
weeks
.append(int(btc_dict
['week']))
weekdays
.append( btc_dict
['weekday'])
#无法将带小数的字符串转化为整数,先将字符串转化为小数,在转化为整数
close
.append(int(float(btc_dict
['close'])))
#实例化Line,x_label_rotation
=20表示旋转
20度 show_minor_x_labels
=False 表示x标签不用都显示
line_chart
=pygal
.Line(x_label_rotation
=20,show_minor_x_labels
=False
)
line_chart
.title
='收盘价对数变化(¥)'
line_chart
.x_labels
= dates
#设置每隔
20显示一次
N=20
line_chart
.x_labels_major
= dates
[::N]
close_log
= [math
.log10(_
) for _
in close
]
#设置纵坐标
line_chart
.add('收盘价',close_log
)
#导出svg格式图像
line_chart
.render_to_file('收盘价折线图.svg')
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]): #
2
y_list
= [v
for _
, v
in y
]
xy_map
.append([x
, sum(y_list
) / len(y_list
)]) #
3
x_unique
, y_mean
= [*zip(*xy_map
)] #
4
line_chart
= pygal
.Line()
line_chart
.title
= title
#line_chart
.x_labels
= x_unique
x_unique_str
=int_str(x_unique
)
line_chart
.x_labels
= x_unique_str
line_chart
.add(y_legend
, y_mean
)
line_chart
.render_to_file(title
+ '.svg')
return line_chart
def
int_str(list_0
):
list_1
=[]
for x
in list_0
:
x_str
=str(x
)
list_1
.append(x_str
)
return list_1
idx_month
= dates
.index('2017-12-01')
line_chart_month
= draw_line(
months
[:idx_month
], close
[:idx_month
], '收盘价月日均值(¥)', '月日均值')
line_chart_month
idx_week
= dates
.index('2017-12-11')
line_chart_week
= draw_line(
weeks
[1:idx_week
], close
[1:idx_week
], '收盘价周日均值(¥)', '周日均值')
line_chart_week
idx_week
= dates
.index('2017-12-11')
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
, close
[1:idx_week
], '收盘价星期均值(¥)', '星期均值')
line_chart_weekday
.x_labels
= ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
line_chart_weekday
.render_to_file('收盘价星期均值(¥).svg')
line_chart_weekday
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')
for svg
in [
'收盘价折线图(¥).svg', '收盘价对数变换折线图(¥).svg', '收盘价月日均值(¥).svg',
'收盘价周日均值(¥).svg', '收盘价星期均值(¥).svg'
]:
html_file
.write(
' <object type="image/svg+xml" data="{0}" height=500></object>\n'.format(svg
)) #
1
html_file
.write('</body></html>')