backend-data-processor
FreeNo executable scriptsNot checkedProcess vehicle insurance Excel data using Pandas - file handling, data cleaning, merging, validation. Use when processing Excel/CSV files, handling data…
by FDU-INS★ 84 GitHub starsAbout this skill
Backend Data Processor - 车险数据处理后端实现指南
版本: v1.0 最后更新: 2025-11-08 适用场景: Excel 数据处理、数据清洗、文件合并、数据验证
📌 Skill 概述
本 Skill 提供车险签单数据处理后端的完整实现指南,作为 analyzing-auto-insurance-data Skill 的后端补充。涵盖数据流转、核心处理逻辑、业务规则实现、性能优化和错误处理。
核心价值:
- ✅ 数据流转架构: Excel → CSV → 合并 → 存储全流程
- ✅ 核心处理函数: 4个关键方法详解(文件处理/清洗/合并/批量扫描)
- ✅ 业务规则实现: 负保费/零手续费/日期标准化/缺失值填充
- ✅ 性能优化技巧: Pandas 最佳实践、内存管理、大文件处理
- ✅ 错误处理与日志: 异常捕获机制、日志标准、用户友好提示
关键文件位置:
- backend/data_processor.py - 数据处理核心
- backend/api_server.py - Flask API 服务
📊 一、数据流转架构
1.1 整体流程图
┌─────────────────────────────────────────────────────────────────┐
│ Excel 文件上传到 data/ 目录 │
└────────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ scan_and_process_new_files() │
│ - 扫描 data/*.xlsx, data/*.xls │
│ - 批量处理所有新文件 │
└────────────────────────────┬────────────────────────────────────┘
│
▼
┌───────────────────────────────────────┐
│ process_new_excel(excel_path) │
│ - 读取 Excel 文件 (pd.read_excel) │
│ - 返回原始 DataFrame │
└───────────────┬───────────────────────┘
│
▼
┌───────────────────────────────────────┐
│ _clean_data(df) │
│ - 删除空行 │
│ - 日期格式标准化 │
│ - 数值类型转换 │
│ - 缺失值填充为空字符串 │
└───────────────┬───────────────────────┘
│
▼
┌───────────────────────────────────────┐
│ merge_with_existing(new_df) │
│ - 读取现有 CSV (如存在) │
│ - pd.concat() 合并新旧数据 │
│ - 根据保单号+投保确认时间去重 │
└───────────────┬───────────────────────┘
│
▼
┌───────────────────────────────────────┐
│ save_merged_data(df) │
│ - 保存为 车险清单_2025年10-11月_合并.csv │
│ - 编码: utf-8-sig (支持Excel打开) │
└───────────────┬───────────────────────┘
│
▼
┌───────────────────────────────────────┐
│ 移动到 data/processed/ 目录 │
│ - 重命名: 原文件名_processed_时间戳.xlsx │
│ - 避免重复处理 │
└───────────────────────────────────────┘
1.2 文件命名与版本管理
输入文件命名规范:
- 格式:
data/*.xlsx或data/*.xls - 示例:
车险清单_2025年10月.xlsx
处理后文件命名:
- 格式:
data/processed/{原文件名}_processed_{YYYYMMDD_HHMMSS}.xlsx - 示例:
data/processed/车险清单_2025年10月_processed_20251108_143025.xlsx
合并文件命名:
- 固定文件名:
车险清单_2025年10-11月_合并.csv - 位置: 项目根目录
- 编码:
utf-8-sig(带 BOM,Excel 可直接打开)
1.3 增量更新策略
去重逻辑 (data_processor.py:172-177):
# 根据保单号和投保确认时间去重,保留最新记录
merged_df = merged_df.drop_duplicates(
subset=['保单号', '投保确认时间'],
keep='last'
)
为什么选择这两个字段:
保单号: 业务唯一标识投保确认时间: 同一保单可能有批改记录,需要保留时间维度
keep='last' 的原因:
- 批改操作会更新保单信息,最新记录更准确
- 合并时新数据在后,
keep='last'优先保留新数据
🔧 二、核心处理函数详解
2.1 process_new_excel(excel_path) - Excel 文件处理
功能: 读取 Excel 文件并返回清洗后的 DataFrame
实现细节:
def process_new_excel(self, excel_path):
print(f"正在处理Excel文件: {excel_path}")
# 读取 Excel 文件
df = pd.read_excel(excel_path)
print(f" 读取成功: {len(df)} 行, {len(df.columns)} 列")
# 数据清洗
df = self._clean_data(df)
return df
注意事项:
- ❌ 不要使用
pd.read_excel(excel_path, dtype=str)- 会导致数值计算失败 - ✅ 推荐: 让 Pandas 自动推断类型,后续在
_clean_data()中转换 - ✅ 异常处理: 调用方
scan_and_process_new_files()已处理异常
常见问题:
- Excel 文件损坏: 会抛出
xlrd.biffh.XLRDError - 文件被占用: Windows 下可能抛出
PermissionError - 内存不足: 大文件(>100MB)可能触发
MemoryError
2.2 _clean_data(df) - 数据清洗
功能: 标准化数据格式,确保符合 CSV 规范
清洗步骤:
步骤 1: 删除完全为空的行
df = df.dropna(how='all')
- 移除 Excel 中的空白行
- 避免后续计算时出现 NaN 值污染
步骤 2: 日期格式标准化
date_columns = ['刷新时间', '投保确认时间', '保险起期']
for col in date_columns:
if col in df.columns:
df[col] = pd.to_datetime(df[col], errors='coerce')
errors='coerce': 无法解析的日期转为NaT(Not a Time)- 支持多种日期格式:
2025-11-08,2025/11/08,20251108等
常见日期问题:
- Excel 日期序列号:
44500→ 自动转换为2021-10-15 - 文本日期: `"2025年
Install backend-data-processor in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
ReadEditGrepGlobFAQ
What does the backend-data-processor skill do?
Process vehicle insurance Excel data using Pandas - file handling, data cleaning, merging, validation. Use when processing Excel/CSV files, handling data imports, implementing business rules (negative premiums, zero commissions), debugging data pipelines, or optimizing Pandas performance. Keywords: data_processor.py, Excel, CSV, Pandas, merge, deduplication, date normalization.
How do I install the backend-data-processor skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the backend-data-processor skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
by Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
by Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
by Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
by Anthropic