Python可视化界面小工具,可自定义历史期数,历史双色球数据,打印结果、写入excel表格、并进行简单的结果分析;
1、工具效果图如下图所示:
2、生成Excel表格数据格式如下图所示:
3、完整代码如下:
# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import requests
import json
import sys
import time
sys.path.append('../')
from tkinter import ttk
from tkinter.ttk import *
from tkinter import *
import tkinter.font as tf
import xlwt
try:
import io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
sys.stdout._CHUNK_SIZE = 1
except:
pass
# 获取实时时间
def now_time():
now = int(time.time()) # 这是时间戳
# 转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(now)
# otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
today = time.strftime("%m%d%H%M%S", timeArray)
return today
def get_history_lotto(pageSize):
"""爬取号码数据"""
url = 'http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&pageNo=1&pageSize={}&systemType=PC'.format(pageSize)
headers = {
'Host': 'www.cwl.gov.cn',
'Referer': 'http://www.cwl.gov.cn/c/2023/01/17/527544.shtml',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
result = json.loads(response.text).get("result")
res_dict = {}
for res in result:
res_dict[res["code"]] = res["red"] +"," + res["blue"]
# print(res_dict)
return res_dict
def cp_res(pageSize):
res_dict = get_history_lotto(pageSize)
today = now_time()
# Excel路径
excel_path = "D://res_dict_{}.xls".format(today)
# 创建新的工作簿
wb = xlwt.Workbook()
#创建新的工作表
she = wb.add_sheet("彩票")
al = xlwt.Alignment()
# 设置水平居中
al.horz = 0x02
# 设置垂直居中
al.vert = 0x01
# 添加边框
borders = xlwt.Borders()
# DASHED:虚线 NO_LINE:没有 THIN:实线
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
#设置背景颜色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
#背景颜色
pattern.pattern_fore_colour = 27
# 黑色字体并居中
# 初始化样式
style0 = xlwt.XFStyle()
style0.alignment = al
# 为样式创建字体
font0 = xlwt.Font()
# 黑体
font0.bold = True
# 0:黑色;2:红色;12:蓝色
font0.colour_index = 0
# 设定样式
style0.font = font0
style0.borders = borders
style0.pattern = pattern
# 红色加粗字体并居中
style01 = xlwt.XFStyle()
style01.alignment = al
font01 = xlwt.Font()
font01.bold = True
font01.colour_index = 2
style01.font = font01
style01.borders = borders
style01.pattern = pattern
# 蓝色加粗字体并居中
style02 = xlwt.XFStyle()
style02.alignment = al
font02 = xlwt.Font()
font02.bold = True
font02.colour_index = 12
style02.font = font02
style02.borders = borders
style02.pattern = pattern
# 蓝色非加粗字体并居中
style = xlwt.XFStyle()
style.alignment = al
font = xlwt.Font()
# font.bold = True
font.colour_index = 12
style.font = font
style.borders = borders
# 红色非加粗字体并居中
# 设置字体在单元格的位置
alignment = xlwt.Alignment()
# 水平方向
alignment.horz = xlwt.Alignment.HORZ_CENTER
# 竖直方向
alignment.vert = xlwt.Alignment.VERT_CENTER
style1 = xlwt.XFStyle()
style1.alignment = alignment
font1 = xlwt.Font()
# font1.bold = True
font1.colour_index = 2
style1.font = font1
style1.borders = borders
# 第一行第一列写入数据
she.write(0, 0, "期数\\彩票", style0)
# 写入红色球数据
for i in range(1,34):
she.write(0, i, i, style01)
# 写入蓝色球数据
for j in range(1,17):
she.write(0, j+33, j, style02)
# 设置列宽
for k in range(1,50):
she.col(k).width = 256 * 3
i=1
for key,value in res_dict.items():
#将字典中的键放在第0列
she.write(i, 0, key, style0)
values = value.split(",")
hong_list = values[0:-1]
lan = int(values[-1])
for hong in hong_list:
hong = int(hong)
#写入数据
she.write(i, hong, hong, style1)
she.write(i,lan+33,lan, style)
i += 1
wb.save(excel_path)
return excel_path
def print_res(pageSize):
res_dict = get_history_lotto(pageSize)
str_res = ""
for key, value in res_dict.items():
hong = " ".join(value.split(","))
str_res += "第"+ key + "期:" + hong + "\n"
return str_res
def analysis_res(pageSize):
res_dict = get_history_lotto(pageSize)
# str_res = ""
hong_list = []
lan_list = []
for key, value in res_dict.items():
hong_list.extend(value.split(",")[:-1])
hong = ",".join(value.split(",")[:-1])
lan = value.split(",")[-1]
lan_list.append(lan)
# str_res += "第"+ key + "期:" +"红色球:" + hong + ",蓝色球:" + lan + "\n"
hong_result = "近{}期红色球解析结果(倒序):\n".format(pageSize)
lan_result = "近{}期蓝色球解析结果(倒序):\n".format(pageSize)
hong_dict = {}
lan_dict = {}
for i in range(1,34):
i = str(i).zfill(2)
hong_dict[i] = hong_list.count(i)
for i in range(1,17):
i = str(i).zfill(2)
lan_dict[i] = lan_list.count(i)
# 红色球&蓝色球根据出现次数进行倒序
hong_dict = sorted(hong_dict.items(), key=lambda x:x[1], reverse=True)
lan_dict = sorted(lan_dict.items(), key=lambda x:x[1], reverse=True)
# print(hong_dict, lan_dict)
for k, v in hong_dict:
hong_result += k + " 出现次数:" + str(v) + "\n"
for k, v in lan_dict:
lan_result += k + " 出现次数:" + str(v) + "\n"
return hong_result + lan_result
def visualization():
# 创建窗口
win = Tk()
win.title("福彩双色球工具")
# win.iconbitmap('*.ico')
win.geometry('580x560')
# win.resizable(0, 0)
# 添加标签控件
def test():
try:
int(label1.get())
print("正确!")
return True
except:
print("错误!")
label1.delete(0, "end")
return False
v = StringVar()
#fleur:十字箭头 heart:心形 man:男人 plus:十字架 mouse:鼠标
label1 = Entry(win, width=22, bg='yellow', bd=3, cursor='heart', textvariable=v, validate="focusout", validatecommand=test)
label1.grid(row=0, column=0,sticky='NW', padx=78, pady=5)
Label(win, text="历史期数(期):").grid(row=0, sticky="W")
# txt.set("在这里输入正整数...")
# label1.insert(0, 50)
Label(win, text="注:只可填入数字,填10表示前10期数据,不填或填错默认为50").grid(row=0, padx=238, pady=5)
# 生成Excel文件按钮绑定执行事件,结果插入Text文本
def save_excel():
text.delete("1.0", "end")
try:
int(label1.get())
res = cp_res(label1.get())
except:
res = cp_res("50")
text.insert('insert', "结果路径:" +res + "\n")
# 打印结果按钮绑定回调函数
def print_result():
text.delete("1.0", "end")
try:
int(label1.get())
res = print_res(label1.get())
except:
res = print_res("50")
font1 = tf.Font(family='微软雅黑',size=10)
text.insert('insert', res + "\n")
for i in range(1, res.count("第") + 1):
text.tag_add("tag1","{}.1".format(i),"{}.8".format(i))
text.tag_add("tag2","{}.10".format(i),"{}.28".format(i))
text.tag_add("tag3","{}.28".format(i),"{}.30".format(i))
text.tag_config("tag1",foreground="green")
text.tag_config("tag2",foreground="red")
text.tag_config("tag3",foreground="blue")
text.config(font=font1)
# 结果解析按钮绑定回调函数
def analysis_result():
text.delete("1.0", "end")
try:
int(label1.get())
res = analysis_res(label1.get())
except:
res = analysis_res("50")
frist = len(res.split("\n")[0])
font1 = tf.Font(family='微软雅黑',size=10)
text.insert('insert', res + "\n")
text.tag_add("tag1", "1.0", "1.{}".format(frist))
text.tag_add("tag2", "35.0", "35.{}".format(frist))
for i in range(2, 35):
text.tag_add("tag3","{}.0".format(i),"{}.2".format(i))
text.tag_add("tag4","{}.8".format(i),"{}.14".format(i))
for i in range(36, 52):
text.tag_add("tag5","{}.0".format(i),"{}.2".format(i))
text.tag_add("tag6","{}.8".format(i),"{}.14".format(i))
text.tag_config("tag1",foreground="red")
text.tag_config("tag2",foreground="blue")
text.tag_config("tag3",foreground="red")
text.tag_config("tag4",foreground="green")
text.tag_config("tag5",foreground="blue")
text.tag_config("tag6",foreground="green")
text.config(font=font1)
# 清除内容按钮绑定回调函数
def clearBox():
text.delete("1.0", "end")
Button(win, text="打印结果", width=8, command=print_result).grid(row=1, column=0, sticky="W", padx=5, pady=5)
Button(win, text="生成Excel文件", width=12, command=save_excel).grid(row=1, column=0, sticky="W", padx=80, pady=5)
Button(win, text="结果解析", command=analysis_result).grid(row=1, column=0, sticky="W", padx=186, pady=5)
Button(win, text="清空内容", command=clearBox).grid(row=1, column=0, sticky="W", padx=256, pady=5)
Label(win, text="注:生成Excel文件更易观察").grid(row=1, padx=318, pady=5)
# 新建文本框
text = Text(win)
# 布局
text.grid(row=2, column=0, sticky="W", padx=5, pady=18)
win.mainloop()
if __name__ == '__main__':
visualization()
优化封装代码:
# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import requests
import json
import sys
import time
sys.path.append('../')
from tkinter import ttk
from tkinter.ttk import *
from tkinter import *
from tkinter.messagebox import *
import tkinter.font as tf
from shuangseqiu import *
from daletou import *
try:
import io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
sys.stdout._CHUNK_SIZE = 1
except:
pass
class ApplicationUi(Frame):
#实现界面生成功能。
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('彩票工具')
self.master.geometry('580x580')
self.createWidgets()
def visualization_ssq(self):
self.TabStrip1__Tab1 = Frame(self.TabStrip1)
self.TabStrip1__Lbl = Label(self.TabStrip1__Tab1, text='双色球工具')
self.TabStrip1__Lbl.place(relx=0.1,rely=0.5)
# 添加标签控件
self.TabStrip1__v = StringVar()
#fleur:十字箭头 heart:心形 man:男人 plus:十字架 mouse:鼠标
Label(self.TabStrip1__Tab1, text="历史期数(期):").place(x = 5, y = 8)
# 创建下拉菜单, 使用 place() 来控制控件的位置
self.TabStrip1_cbox = ttk.Combobox(self.TabStrip1__Tab1)
self.TabStrip1_cbox.place(x = 85, y = 8)
# 设置下拉菜单中的值 get_log_path()
self.TabStrip1_cbox['value'] = ["10","20","50","100","200"]
self.TabStrip1_cbox["width"] = 15
#通过 current() 设置下拉菜单选项的默认值
try:
self.TabStrip1_cbox.current(3)
except:
pass
# txt.set("在这里输入正整数...")
# self.TabStrip1_cbox.insert(0, 50)
Label(self.TabStrip1__Tab1, text="注:可自定义填入数字").place(x = 250, y = 8)
# 生成Excel文件按钮绑定执行事件,结果插入Text文本
def save_excel():
self.text.delete("1.0", "end")
try:
int(self.TabStrip1_cbox.get())
res = cp_res(self.TabStrip1_cbox.get())
except:
res = cp_res("50")
self.text.insert('insert', "双色球结果路径:" +res + "\n")
# 打印结果按钮绑定回调函数
def print_result():
self.text.delete("1.0", "end")
try:
int(self.TabStrip1_cbox.get())
res = print_res(self.TabStrip1_cbox.get())
except:
res = print_res("50")
font1 = tf.Font(family='微软雅黑',size=10)
self.text.insert('insert', res + "\n")
for i in range(1, res.count("第") + 1):
self.text.tag_add("tag1","{}.1".format(i),"{}.8".format(i))
self.text.tag_add("tag2","{}.10".format(i),"{}.28".format(i))
self.text.tag_add("tag3","{}.28".format(i),"{}.30".format(i))
self.text.tag_config("tag1",foreground="green")
self.text.tag_config("tag2",foreground="red")
self.text.tag_config("tag3",foreground="blue")
self.text.config(font=font1)
# 结果解析按钮绑定回调函数
def analysis_result():
self.text.delete("1.0", "end")
try:
int(self.TabStrip1_cbox.get())
res = analysis_res(self.TabStrip1_cbox.get())
except:
res = analysis_res("50")
frist = len(res.split("\n")[0])
font1 = tf.Font(family='微软雅黑',size=10)
self.text.insert('insert', res + "\n")
self.text.tag_add("tag1", "1.0", "1.{}".format(frist))
self.text.tag_add("tag2", "35.0", "35.{}".format(frist))
for i in range(2, 35):
self.text.tag_add("tag3","{}.0".format(i),"{}.2".format(i))
self.text.tag_add("tag4","{}.8".format(i),"{}.14".format(i))
for i in range(36, 52):
self.text.tag_add("tag5","{}.0".format(i),"{}.2".format(i))
self.text.tag_add("tag6","{}.8".format(i),"{}.14".format(i))
self.text.tag_config("tag1",foreground="red")
self.text.tag_config("tag2",foreground="blue")
self.text.tag_config("tag3",foreground="red")
self.text.tag_config("tag4",foreground="green")
self.text.tag_config("tag5",foreground="blue")
self.text.tag_config("tag6",foreground="green")
self.text.config(font=font1)
# 清除内容按钮绑定回调函数
def clearBox():
self.text.delete("1.0", "end")
Button(self.TabStrip1__Tab1, text="打印结果", width=8, command=print_result).place(x=5, y=35)
Button(self.TabStrip1__Tab1, text="生成Excel文件", width=12, command=save_excel).place(x=80, y=35)
Button(self.TabStrip1__Tab1, text="结果解析", command=analysis_result).place(x=186, y=35)
Button(self.TabStrip1__Tab1, text="清空内容", command=clearBox).place(x=256, y=35)
Label(self.TabStrip1__Tab1, text="注:生成Excel文件更易观察").place(x=320, y=38)
# 新建文本框
self.text = Text(self.TabStrip1__Tab1, width=500)
# 布局
self.text.grid(pady=70)
self.TabStrip1.add(self.TabStrip1__Tab1, text='双色球工具')
def visualization_dlt(self):
self.TabStrip2__Tab1 = Frame(self.TabStrip1)
self.TabStrip2__Lbl = Label(self.TabStrip2__Tab1, text='大乐透工具')
self.TabStrip2__Lbl.place(relx=0.1,rely=0.5)
# 添加标签控件
self.TabStrip2__v = StringVar()
#fleur:十字箭头 heart:心形 man:男人 plus:十字架 mouse:鼠标
Label(self.TabStrip2__Tab1, text="历史期数(期):").place(x = 5, y = 8)
# 创建下拉菜单, 使用 place() 来控制控件的位置
self.TabStrip2_cbox = ttk.Combobox(self.TabStrip2__Tab1)
self.TabStrip2_cbox.place(x = 85, y = 8)
# 设置下拉菜单中的值 get_log_path()
self.TabStrip2_cbox['value'] = ["10","20","50","100","200"]
self.TabStrip2_cbox["width"] = 15
#通过 current() 设置下拉菜单选项的默认值
try:
self.TabStrip2_cbox.current(3)
except:
pass
# txt.set("在这里输入正整数...")
# self.TabStrip2_cbox.insert(0, 50)
Label(self.TabStrip2__Tab1, text="注:可自定义填入数字").place(x = 250, y = 8)
# 生成Excel文件按钮绑定执行事件,结果插入Text文本
def save_excel():
self.text2.delete("1.0", "end")
try:
res = cp_dlres(self.TabStrip2_cbox.get())
except:
res = cp_dlres("50")
self.text2.insert('insert', "大乐透结果路径:" +res + "\n")
self.text2.insert("-----------------------------------------")
# 打印结果按钮绑定回调函数
def print_result():
self.text2.delete("1.0", "end")
try:
int(self.TabStrip2_cbox.get())
res = print_dlres(self.TabStrip2_cbox.get())
except:
res = print_dlres("50")
font1 = tf.Font(family='微软雅黑',size=10)
self.text2.insert('insert', res + "\n")
for i in range(1, res.count("第") + 1):
# 第23018期:04 08 17 26 30 03 11
self.text2.tag_add("tag1","{}.1".format(i),"{}.6".format(i))
self.text2.tag_add("tag2","{}.8".format(i),"{}.22".format(i))
self.text2.tag_add("tag3","{}.23".format(i),"{}.28".format(i))
self.text2.tag_config("tag1",foreground="green")
self.text2.tag_config("tag2",foreground="red")
self.text2.tag_config("tag3",foreground="blue")
self.text2.config(font=font1)
# 结果解析按钮绑定回调函数
def analysis_result():
self.text2.delete("1.0", "end")
try:
int(self.TabStrip2_cbox.get())
res = analysis_dlres(self.TabStrip2_cbox.get())
except:
res = analysis_dlres("50")
frist = len(res.split("\n")[0])
font1 = tf.Font(family='微软雅黑',size=10)
self.text2.insert('insert', res + "\n")
self.text2.tag_add("tag1", "1.0", "1.{}".format(frist))
self.text2.tag_add("tag2", "37.0", "37.{}".format(frist))
for i in range(2, 37):
self.text2.tag_add("tag3","{}.0".format(i),"{}.2".format(i))
self.text2.tag_add("tag4","{}.8".format(i),"{}.14".format(i))
for i in range(38, 52):
self.text2.tag_add("tag5","{}.0".format(i),"{}.2".format(i))
self.text2.tag_add("tag6","{}.8".format(i),"{}.14".format(i))
self.text2.tag_config("tag1",foreground="red")
self.text2.tag_config("tag2",foreground="blue")
self.text2.tag_config("tag3",foreground="red")
self.text2.tag_config("tag4",foreground="green")
self.text2.tag_config("tag5",foreground="blue")
self.text2.tag_config("tag6",foreground="green")
self.text2.config(font=font1)
# 清除内容按钮绑定回调函数
def clearBox():
self.text2.delete("1.0", "end")
Button(self.TabStrip2__Tab1, text="打印结果", width=8, command=print_result).place(x=5, y=35)
Button(self.TabStrip2__Tab1, text="生成Excel文件", width=12, command=save_excel).place(x=80, y=35)
Button(self.TabStrip2__Tab1, text="结果解析", command=analysis_result).place(x=186, y=35)
Button(self.TabStrip2__Tab1, text="清空内容", command=clearBox).place(x=256, y=35)
Label(self.TabStrip2__Tab1, text="注:生成Excel文件更易观察").place(x=320, y=38)
# 新建文本框
self.text2 = Text(self.TabStrip2__Tab1, width=500)
# 布局
self.text2.grid(pady=70)
self.TabStrip1.add(self.TabStrip2__Tab1, text='大乐透工具')
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
self.TabStrip1 = Notebook(self.top)
self.TabStrip1.place(relx=0.009, rely=0.009, relwidth=0.991, relheight=0.991)
self.visualization_ssq()
self.visualization_dlt()
if __name__ == '__main__':
top = Tk()
ApplicationUi(top).mainloop()
大乐透代码:
# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import requests
import json
import sys
import time
sys.path.append('../')
import xlwt
try:
import io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
sys.stdout._CHUNK_SIZE = 1
except:
pass
# 获取实时时间
def now_time():
now = int(time.time()) # 这是时间戳
# 转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(now)
# otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
today = time.strftime("%m%d%H%M%S", timeArray)
return today
def get_history_dllotto(pageSize):
"""爬取号码数据 35选5 + 12选2"""
url = 'https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize={}'.format(pageSize)
headers = {
'Host': 'webapi.sporttery.cn',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
result_list = json.loads(response.text).get("value").get("list")
res_dict = {}
for res in result_list:
res_dict[res["lotteryDrawNum"]] = res["lotteryDrawResult"]
return res_dict
def cp_dlres(pageSize):
res_dict = get_history_dllotto(pageSize)
today = now_time()
# Excel路径
excel_path = "D://dlt_res_dict_{}.xls".format(today)
# 创建新的工作簿
wb = xlwt.Workbook()
#创建新的工作表
she = wb.add_sheet("大乐透")
al = xlwt.Alignment()
# 设置水平居中
al.horz = 0x02
# 设置垂直居中
al.vert = 0x01
# 添加边框
borders = xlwt.Borders()
# DASHED:虚线 NO_LINE:没有 THIN:实线
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
#设置背景颜色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
#背景颜色
pattern.pattern_fore_colour = 27
# 黑色字体并居中
# 初始化样式
style0 = xlwt.XFStyle()
style0.alignment = al
# 为样式创建字体
font0 = xlwt.Font()
# 黑体
font0.bold = True
# 0:黑色;2:红色;12:蓝色
font0.colour_index = 0
# 设定样式
style0.font = font0
style0.borders = borders
style0.pattern = pattern
# 红色加粗字体并居中
style01 = xlwt.XFStyle()
style01.alignment = al
font01 = xlwt.Font()
font01.bold = True
font01.colour_index = 2
style01.font = font01
style01.borders = borders
style01.pattern = pattern
# 蓝色加粗字体并居中
style02 = xlwt.XFStyle()
style02.alignment = al
font02 = xlwt.Font()
font02.bold = True
font02.colour_index = 12
style02.font = font02
style02.borders = borders
style02.pattern = pattern
# 蓝色非加粗字体并居中
style = xlwt.XFStyle()
style.alignment = al
font = xlwt.Font()
# font.bold = True
font.colour_index = 12
style.font = font
style.borders = borders
# 红色非加粗字体并居中
# 设置字体在单元格的位置
alignment = xlwt.Alignment()
# 水平方向
alignment.horz = xlwt.Alignment.HORZ_CENTER
# 竖直方向
alignment.vert = xlwt.Alignment.VERT_CENTER
style1 = xlwt.XFStyle()
style1.alignment = alignment
font1 = xlwt.Font()
# font1.bold = True
font1.colour_index = 2
style1.font = font1
style1.borders = borders
# 第一行第一列写入数据
she.write(0, 0, "期数\\彩票", style0)
# 写入前区红球数据
for i in range(1,36):
she.write(0, i, i, style01)
# 写入后区蓝球数据
for j in range(1,13):
she.write(0, j+35, j, style02)
# 设置列宽
for k in range(1,49):
she.col(k).width = 256 * 3
i=1
for key,value in res_dict.items():
#将字典中的键放在第0列
she.write(i, 0, key, style0)
values = value.split(" ")
hong_list = values[0:-2]
lan_list = values[-2:]
for hong in hong_list:
hong = int(hong)
#写入数据
she.write(i, hong, hong, style1)
for lan in lan_list:
lan = int(lan)
she.write(i, lan+35, lan, style)
i += 1
wb.save(excel_path)
return excel_path
def print_dlres(pageSize):
res_dict = get_history_dllotto(pageSize)
str_res = ""
for key, value in res_dict.items():
str_res += "第"+ key + "期:" + value + "\n"
return str_res
def analysis_dlres(pageSize):
res_dict = get_history_dllotto(pageSize)
hong_list = []
lan_list = []
for key, value in res_dict.items():
hong_list.extend(value.split(" ")[:-2])
lan = value.split(" ")[-2:]
lan_list.extend(lan)
# str_res += "第"+ key + "期:" +"红色球:" + hong + ",蓝色球:" + lan + "\n"
hong_result = "近{}期前区红球解析结果(倒序):\n".format(pageSize)
lan_result = "近{}期后区蓝球解析结果(倒序):\n".format(pageSize)
hong_dict = {}
lan_dict = {}
for i in range(1,36):
i = str(i).zfill(2)
hong_dict[i] = hong_list.count(i)
for i in range(1,13):
i = str(i).zfill(2)
lan_dict[i] = lan_list.count(i)
# 红色球&蓝色球根据出现次数进行倒序
hong_dict = sorted(hong_dict.items(), key=lambda x:x[1], reverse=True)
lan_dict = sorted(lan_dict.items(), key=lambda x:x[1], reverse=True)
# print(hong_dict, lan_dict)
for k, v in hong_dict:
hong_result += k + " 出现次数:" + str(v) + "\n"
for k, v in lan_dict:
lan_result += k + " 出现次数:" + str(v) + "\n"
return hong_result + lan_result
if __name__ == '__main__':
cp_dlres(10)