第9讲:OpenClaw 实战:开发数据处理 Skill

第9讲:OpenClaw 实战:开发数据处理 Skill

动手开发一个完整的数据处理 Skill,掌握 OpenClaw 实战技巧。

一、项目目标

开发「数据分析师」Skill,功能包括:

  • 📊 数据清洗和预处理
  • 📈 数据统计分析
  • 📉 可视化图表生成
  • 📄 分析报告导出

二、项目结构

1
2
3
4
5
6
7
8
9
10
11
12
13
data-analyst-skill/
├── skill.yaml
├── main.py
├── tools/
│ ├── __init__.py
│ ├── data_cleaner.py
│ ├── analyzer.py
│ └── visualizer.py
├── templates/
│ └── report_template.html
├── requirements.txt
└── tests/
└── test_analyzer.py

三、核心代码

3.1 Skill 主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# main.py
from openclaw import Skill
from tools.data_cleaner import DataCleaner
from tools.analyzer import DataAnalyzer
from tools.visualizer import Visualizer

class DataAnalystSkill(Skill):
def __init__(self):
super().__init__()
self.cleaner = DataCleaner()
self.analyzer = DataAnalyzer()
self.visualizer = Visualizer()

self.register_intent("analyze_data", self.analyze)
self.register_intent("clean_data", self.clean)
self.register_intent("visualize", self.visualize)

def analyze(self, file_path, analysis_type="overview"):
"""数据分析"""
df = self.cleaner.load(file_path)
result = self.analyzer.analyze(df, analysis_type)
return result

def clean(self, file_path, operations=None):
"""数据清洗"""
df = self.cleaner.load(file_path)
df = self.cleaner.clean(df, operations)
return {"cleaned_file": self.cleaner.save(df)}

def visualize(self, file_path, chart_type="bar"):
"""数据可视化"""
df = self.cleaner.load(file_path)
chart = self.visualizer.create_chart(df, chart_type)
return {"chart": chart}

if __name__ == "__main__":
skill = DataAnalystSkill()
skill.run()

3.2 数据清洗工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# tools/data_cleaner.py
import pandas as pd
import numpy as np

class DataCleaner:
def load(self, file_path):
"""加载数据"""
if file_path.endswith('.csv'):
return pd.read_csv(file_path)
return pd.read_excel(file_path)

def clean(self, df, operations):
"""清洗数据"""
if 'remove_duplicates' in operations:
df = df.drop_duplicates()

if 'fill_na' in operations:
df = df.fillna(df.mean())

if 'remove_outliers' in operations:
for col in df.select_dtypes(include=[np.number]):
Q1 = df[col].quantile(0.25)
Q3 = df[col].quantile(0.75)
IQR = Q3 - Q1
df = df[~((df[col] < (Q1 - 1.5 * IQR)) | (df[col] > (Q3 + 1.5 * IQR)))]

return df

def save(self, df, output_path="cleaned_data.xlsx"):
df.to_excel(output_path, index=False)
return output_path

四、运行和测试

1
2
3
4
5
6
7
8
# 安装依赖
pip install -r requirements.txt

# 运行
openclaw run

# 测试
openclaw test

五、发布到 ClawHub

1
2
3
4
5
# 打包
openclaw package

# 发布
openclaw publish --version 1.0.0

加入学习群

👉 加入AI编程学习交流群

点击加入


本讲是《Skills 从入门到实践》系列课程的第9讲。

🎓 AI 编程实战课程

想系统学习 AI 编程?程序员晚枫的 AI 编程实战课 帮你从零上手!