表格简单表示机器学习 excel机器
转载
import pandas as pd
import requests
import json
import hmac
import hashlib
import base64
import urllib
import time
class SecretRobot:
def __init__(self, secret='', webhook=''):
self.secret = secret
self.webhook = webhook
self.timestamp = str(round(time.time() * 1000))
self.headers = {"Content-Type": "application/json"}
def send(self, message):
self.get_sign()
webhook = self.webhook + '×tamp=' + self.timestamp + '&sign=' + self.sign
data = {
"msgtype": "text",
"text": {
"content": message
}
}
resp = requests.post(webhook, data=json.dumps(data), headers=self.headers)
resp.close()
def get_sign(self):
secret_enc = self.secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(self.timestamp, self.secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
self.sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
def send_dingding_message1(message, secret):
webhook = 'XXX'
# 创建 SecretRobot 对象
robot = SecretRobot(secret=secret, webhook=webhook)
# 发送消息
robot.send(message)
df = pd.read_excel(r"D:\每天店铺信息获取\智汇_美客多信息覆盖情况_2023-09-13_02-45-54.xlsx")
# 统计销售名称在数据列中出现的次数
sale_counts = {}
sales=df['销售名称']
for sale in sales:
if sale not in sale_counts:
sale_counts[sale] = 0
sale_counts[sale] += 1
listing_coverage = df['listing覆盖率'].tolist()
promotion_coverage = df['promotion覆盖率'].tolist()
# 统计低于阈值的店铺数目
threshold_listing = 0.5
threshold_promotion = 0.3
threshold_promotion1 = 0.5
total_shops = len(df)
total_low_listing = sum(coverage < threshold_listing and coverage != 0 for coverage in listing_coverage)
total_low_promotion = sum(coverage < threshold_promotion and coverage != 0 for coverage in promotion_coverage)
total_low_promotion_over_50 = sum(coverage < threshold_promotion1 and coverage != 0 for coverage in promotion_coverage)
# 根据销售名称统计覆盖率低的店铺数量
sales = df['销售名称'].tolist()
low_coverage_shop_counts = {}
for sale in set(sales):
filtered_df = df[df['销售名称'] == sale]
low_listing_count = sum(coverage < threshold_listing and coverage != 0 for coverage in filtered_df['listing覆盖率'])
low_promotion_count = sum(coverage < threshold_promotion and coverage != 0 for coverage in filtered_df['promotion覆盖率'])
low_promotion_count1 = sum(coverage < threshold_promotion1 and coverage != 0 for coverage in filtered_df['promotion覆盖率'])
if sale not in low_coverage_shop_counts:
low_coverage_shop_counts[sale] = {'listing': 0, 'promotion': 0,'promotion1':0}
low_coverage_shop_counts[sale]['listing'] += low_listing_count
low_coverage_shop_counts[sale]['promotion'] += low_promotion_count
low_coverage_shop_counts[sale]['promotion1'] += low_promotion_count1
# 添加销售名称对应的低覆盖率店铺数量到统计结果
msg1 = f"参与计算的<店铺+站点>共有{total_shops}个,其中"
for sale, count in sale_counts.items():
msg1 += f"{sale}{count}个,"
msg1 = msg1[:-1] + "\n" # 去除最后一个逗号并添加换行符
msg1 += f" 今日listing覆盖率低于{threshold_listing*100}%的店铺有{total_low_listing}个,"
for sale, counts in low_coverage_shop_counts.items():
msg1 += f"其中{sale}{counts['listing']}个。"
msg1 = msg1 + "\n"
msg1 += f" 今日promotion覆盖率低于{threshold_promotion*100}%的店铺有{total_low_promotion}个,"
for sale, counts in low_coverage_shop_counts.items():
msg1 += f"其中{sale}{counts['promotion']}个。"
msg1 = msg1 + "\n"
msg1 += f" 今日promotion覆盖率低于50%的店铺有{total_low_promotion_over_50}个,"
for sale, counts in low_coverage_shop_counts.items():
msg1 += f"其中{sale}{counts['promotion1']}个。"
secret1 = 'XXX'
send_dingding_message1(msg1,secret1)
- 导入所需的库:pandas用于读取Excel文件,requests用于发送HTTP请求,json用于处理JSON数据,hmac、hashlib、base64、urllib和time用于生成签名和时间戳。
- 创建SecretRobot类,用于发送钉钉消息。初始化方法__init__()接收两个参数:secret和webhook,分别表示钉钉机器人的密钥和Webhook地址。timestamp表示当前时间戳,headers表示请求头。
- send()方法用于发送消息。首先调用get_sign()方法生成签名,然后将签名和时间戳拼接到webhook上。接着构建消息内容data,以文本类型msgtype发送消息。最后通过requests库发送POST请求,关闭响应。
- get_sign()方法用于生成签名。使用密钥编码为字节流secret_enc,并将时间戳和密钥拼接为待签名字符串string_to_sign。将待签名字符串编码为字节流string_to_sign_enc,使用hmac库和sha256哈希算法生成hmac_code。对hmac_code进行base64编码并进行URL编码,得到签名self.sign。
- 定义send_dingding_message1()函数,接收两个参数:message表示要发送的消息内容,secret表示钉钉机器人的密钥。创建SecretRobot对象robot,并传入密钥和webhook参数。调用robot的send()方法发送消息。
- 使用pandas库读取Excel文件,获取销售名称和覆盖率数据。
- 统计销售名称在数据列中出现的次数,存储在字典sale_counts中。
- 将listing覆盖率和promotion覆盖率数据转换为列表。
- 统计低于阈值的店铺数目,包括总店铺数、低于listing覆盖率阈值的店铺数、低于promotion覆盖率阈值的店铺数以及低于50% promotion覆盖率阈值的店铺数。
- 根据销售名称统计低覆盖率店铺数量,存储在字典low_coverage_shop_counts中。
- 构建消息内容msg1,包括总店铺数、每个销售名称对应的店铺数、低于listing覆盖率阈值的店铺数和具体销售名称下的店铺数、低于promotion覆盖率阈值的店铺数和具体销售名称下的店铺数、低于50% promotion覆盖率阈值的店铺数和具体销售名称下的店铺数。
- 调用send_dingding_message1()函数发送消息,传入消息内容和钉钉机器人的密钥。
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。