测试脚本
2025/3/26原创大约 4 分钟约 1345 字
测试脚本
import json
import pandas as pd
import requests
from deepseek.DeepSeekCommentParser import DeepSeekCommentParser
# # 1. 读取数据
# df = pd.read_excel(f"D:\我的资料库\Documents\Downloads\工作簿1.xlsx")
#
# # 2. 提取关键信息(如前5行)
# data_sample = df.head().to_string()
#
# # 3. 调用 DeepSeek API(假设 API 支持文本分析)
# api_url = "https://api.deepseek.com/chat/completions"
# headers = {"Authorization": "sk-63344862967e44aa9fb34301a5cb0cf4","Content-Type": "application/json"}
# payload = {
# "messages": [
# {"role": "user", "content": f"请分析以下数据:\n {data_sample}"}
# ]
# }
# print(payload)
headers = {
"Content-Type": "application/json"
}
response = requests.post("http://localhost:11434/api/chat", json={
"model": "deepseek-r1:latest",
"done": True,
"options": {
"temperature": 0.8,
"top_p": 0.9,
"max_tokens": 1024
},
"messages": [
{"role": "user", "content": "请分析以下数据:\n 月份 销售\n 1 100\n 2 300\n 3 200"}
]
}, headers=headers, stream=True)
msg=""
for line in response.iter_lines():
if line:
msg+=json.loads(line.decode("utf-8"))["message"]["content"]
print(msg)
parser = DeepSeekCommentParser()
markdown_content = parser.parse_to_markdown(msg)
print("生成的Markdown内容:\n")
print(markdown_content)
parser.save_as_file(msg, "deepseek_docs.md")
# for s in response.text.split():
# try:
# print(s)
# # print(json.loads(s)["message"]["content"])
# except:
# pass
# response = requests.post(api_url, json=payload, headers=headers)
#
# # 4. 获取回答
# 4. 获取回答
# answer = response.json()["choices"][0]["message"]["content"]
#
# # 5. 输出回答
# print(answer)
文档转换
import re
from datetime import datetime
class DeepSeekCommentParser:
def __init__(self):
self.section_pattern = re.compile(r'^#+\s*(.*?)\s*#*$') # 匹配标题
self.code_pattern = re.compile(r'```(.*?)\n(.*?)```', re.DOTALL) # 匹配代码块
self.list_pattern = re.compile(r'^\s*[\-\*\+]\s+(.*)$', re.MULTILINE) # 匹配列表项
def parse_to_markdown(self, comment_text, title="DeepSeek Documentation"):
"""
将DeepSeek注释转换为Markdown文档
参数:
comment_text: 包含DeepSeek注释的文本
title: 文档标题
返回:
Markdown格式的字符串
"""
# 添加文档标题和生成时间
md_content = f"# {title}\n\n"
md_content += f"*Generated from DeepSeek comments on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*\n\n"
# 分割注释为段落
paragraphs = [p.strip() for p in comment_text.split('\n\n') if p.strip()]
for para in paragraphs:
# 检查是否是标题
section_match = self.section_pattern.match(para)
if section_match:
md_content += f"\n## {section_match.group(1)}\n\n"
continue
# 检查是否是代码块
code_match = self.code_pattern.search(para)
if code_match:
language = code_match.group(1) or 'python'
code = code_match.group(2).strip()
md_content += f"```{language}\n{code}\n```\n\n"
continue
# 检查是否是列表
if self.list_pattern.search(para):
list_items = self.list_pattern.findall(para)
md_content += '\n'.join(f"- {item}" for item in list_items) + '\n\n'
continue
# 普通段落
md_content += f"{para}\n\n"
return md_content
def save_as_file(self, comment_text, output_path, format='md'):
"""将解析结果保存到文件"""
if format.lower() == 'md':
content = self.parse_to_markdown(comment_text)
with open(output_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Markdown文档已保存至: {output_path}")
else:
raise ValueError(f"不支持的格式: {format}")
# 使用示例
if __name__ == "__main__":
# 示例DeepSeek注释
deepseek_comment = """
# 函数说明 #
这是一个示例函数,用于演示DeepSeek注释解析
## 参数 ##
- param1: 第一个参数,类型为字符串
- param2: 第二个参数,类型为整数
## 返回值 ##
返回处理后的字符串结果
## 示例代码 ##
```python
def example_func(param1, param2):
return f"{param1}-{param2}"
```
"""
parser = DeepSeekCommentParser()
markdown_content = parser.parse_to_markdown(deepseek_comment)
print("生成的Markdown内容:\n")
print(markdown_content)
# 保存到文件
parser.save_as_file(deepseek_comment, "deepseek_docs.md")
生成的Markdown内容
# DeepSeek Documentation
*Generated from DeepSeek comments on 2025-03-25 22:29:20*
<think>
首先,我观察到了一个包含三个月销售数据的表格。
月份分别是1月、2月和3月,对应的销量是100、300和200。为了更直观地了解这些数据的变化趋势,我可以将这些数据绘制为折线图或条形图。这样,我可以更容易看到每个月销量的增减情况。
接下来,我计划计算销量的增长率,以分析每个月份相对于上一个月的销售变化百分比。使用公式:增长率 = (当前月销量 - 上个月销量) / 上个月销量 × 100%。
首先计算2月份相对于1月份的增长率:
(300 - 100)/ 100 × 100% = 200%,这意味着2月的销量比1月增长了200%。
然后是3月份相对于2月份的增长率:
(200 - 300)/ 300 × 100% ≈ -33.33%,表示3月的销量比2月减少了约33.33%。
最后,我考虑这些增长率是否符合预期,或者是否存在异常情况。根据计算结果,可以看出销量在2月份显著增长,在3月份有所下降。这可能反映了市场状况的变化或其他外部因素的影响。
通过这些分析,可以更深入地理解销售数据的变动规律,并为进一步的数据预测和决策提供依据。
</think>
好的,让我们来分析一下这个月份销售数据:
### 销售数据:
| 月份 | 销售 |
|------|-----|
| 1 | 100 |
| 2 | 300 |
| 3 | 200 |
## 分析步骤:
- 1月的销售为100。
- 2月的销售增加到300,比上个月增加了200个单位(+200%)。
- 3月的销售减少到200,比上个月减少了100个单位(约-33.33%)。
- **从1月到2月的增长率:**
- **从2月到3月的增长率:**
- 销售额在2月份显著增长了200%,可能是因为市场需求增加或推广策略有效。
- 在3月份,销售额下降了约33.33%,这可能反映了市场竞争加剧、产品季节性不景气或其他外部因素的影响。
通过以上分析,我们可以更好地理解销售数据的变动规律,并为后续的市场策略和销售预测提供参考依据。
正则解析图表
import re
pattern = r'## 完整代码.*?python.*```'
match = re.search(pattern, markdown_content, re.DOTALL)
print(match)
if match:
code_content = match.group(0).strip()
print("匹配到的代码内容:")
print(code_content.replace("## 完整代码","").replace("```python","").replace("```","").strip())
with open("code.py", 'w', encoding='utf-8') as f:
f.write(code_content.replace("## 完整代码","").replace("```python","").replace("```","").strip())
import sys
import os
os.system('python code.py')
else:
print("未找到匹配内容")