ai_member_xiaoyan/add_month_week.py

70 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
# 文件路径
file_path = r'/root/.openclaw/workspace-xiaoyan/business_knowledge/新知识库初版/全包词汇/L2知识库-三级+A2.xlsx'
# 读取文件
df = pd.read_excel(file_path)
print("现有表格的列名:")
print(df.columns.tolist())
print("\n现有表格的前5行")
print(df.head())
print(f"\n现有表格总行数:{len(df)}")
# 定义要添加的单词、词性和词义
words_to_add = [
# 月份
("January", "n", "一月"),
("February", "n", "二月"),
("March", "n", "三月"),
("April", "n", "四月"),
("May", "n", "五月"),
("June", "n", "六月"),
("July", "n", "七月"),
("August", "n", "八月"),
("September", "n", "九月"),
("October", "n", "十月"),
("November", "n", "十一月"),
("December", "n", "十二月"),
# 星期
("Monday", "n", "星期一"),
("Tuesday", "n", "星期二"),
("Wednesday", "n", "星期三"),
("Thursday", "n", "星期四"),
("Friday", "n", "星期五"),
("Saturday", "n", "星期六"),
("Sunday", "n", "星期日"),
]
# 按字母顺序排序
words_to_add_sorted = sorted(words_to_add, key=lambda x: x[0].lower())
print("\n" + "="*80)
print("要添加的单词(按字母顺序):")
for word, pos, meaning in words_to_add_sorted:
print(f"{word:15s} {pos:5s} {meaning}")
# 创建新行的DataFrame
new_rows = pd.DataFrame(words_to_add_sorted, columns=['单词', '词性', '中文释义'])
# 填充其他列(为空)
for col in df.columns:
if col not in ['单词', '词性', '中文释义']:
new_rows[col] = ""
# 合并到原数据
df_updated = pd.concat([df, new_rows], ignore_index=True)
# 按单词字母顺序排序
df_updated = df_updated.sort_values(by='单词', key=lambda x: x.str.lower())
# 保存结果
df_updated.to_excel(file_path, index=False)
print("\n" + "="*80)
print(f"已添加 {len(new_rows)} 个单词")
print(f"更新后表格总行数:{len(df_updated)}")
print(f"已保存到:{file_path}")