欢迎光临
我们一直在努力

影刀RPA 流程结果的数据校验:从格式检查到业务逻辑验证

影刀RPA 流程结果的数据校验:从格式检查到业务逻辑验证

在这里插入图片描述

作者:林焱

什么情况用这个

在这里插入图片描述

流程跑完了,Excel也生成了,数据看起来也正常——直到有人发现里面有负数金额、有重复订单、有2025年13月32日这种不存在的日期。

流程"不报错"不等于"结果正确"。很多数据问题是静默的——流程顺利跑完了,但数据是错的。你需要一套结果校验机制,在数据对外使用之前把关。 在这里插入图片描述

拼多多店群自动化报活动上架!

怎么做

格式校验

在这里插入图片描述

def validate_output_format(df):
errors = []

# 检查必填字段不为空
required_cols = ["订单号", "金额", "日期"]
for col in required_cols:
if col not in df.columns:
errors.append(f"缺少列: {col}")
else:
empty_count = df[col].isna().sum()
if empty_count > 0:
errors.append(f"列'{col}'有{empty_count}个空值")

# 检查数据类型
if "金额" in df.columns:
non_numeric = df[pd.to_numeric(df["金额"], errors='coerce').isna()]
if len(non_numeric) > 0:
errors.append(f"金额列有{len(non_numeric)}个非数字值")

return errors

业务逻辑校验

在这里插入图片描述

def validate_business_rules(df):
errors = []

# 金额不能为负
if "金额" in df.columns:
negative = df[df["金额"].astype(float) < 0]
if len(negative) > 0:
errors.append(f"发现{len(negative)}条负金额")

# 日期合理性
if "日期" in df.columns:
future = df[pd.to_datetime(df["日期"]) > datetime.now()]
if len(future) > 0:
errors.append(f"发现{len(future)}条未来日期")

# 唯一性
if "订单号" in df.columns:
dupes = df[df["订单号"].duplicated()]
if len(dupes) > 0:
errors.append(f"发现{len(dupes)}条重复订单号")

# 合计校验
if "金额" in df.columns and hasattr(df, "expected_total"):
actual_total = df["金额"].astype(float).sum()
if abs(actual_total df.expected_total) > 0.01:
errors.append(f"合计不匹配: 实际{actual_total} vs 预期{df.expected_total}")

return errors

比对校验

在这里插入图片描述

def compare_with_previous(file_new, file_old):
"""与上一次结果比对——异常波动检测"""
df_new = pd.read_excel(file_new)
df_old = pd.read_excel(file_old)

# 数量对比
diff_pct = abs(len(df_new) len(df_old)) / len(df_old)
if diff_pct > 0.3: # 30%以上波动
print(f"⚠️ 数据量波动{diff_pct:.1%}: {len(df_old)}{len(df_new)}")

有什么坑

在这里插入图片描述

坑一:校验太严格正常业务被误拦

TEMU店群矩阵自动化运营核价报活动

在这里插入图片描述

现象:金额为0的免费商品被当成了异常。

解决:校验规则要区分"一定是错的"和"可能有问题需要确认"两级,后者只告警不拦截。

在这里插入图片描述

坑二:校验代码本身有Bug

现象:校验代码的Bug导致正常数据被判定为异常,流程白跑。

在这里插入图片描述

解决:校验逻辑本身也要测试——用一组包含各种边界情况的测试数据验证校验函数。


总结:结果校验的三个层次——格式对不对(必填、类型)→ 逻辑合不合理(范围、唯一性)→ 横向比一比(与历史对比)。上线前至少做格式校验和关键字段的业务逻辑校验。

赞(0)
未经允许不得转载:171主机测评 » 影刀RPA 流程结果的数据校验:从格式检查到业务逻辑验证
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址