diff --git a/.gitignore b/.gitignore index 22016a7..7aa219e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,12 @@ -__pycache__ +# 忽略 Python 缓存目录 +__pycache__/ -/.vscode/* +# 忽略 VSCode 配置目录 +/.vscode/ +/.history/ +# 忽略特定文件 writemdict -.history stardict.csv stardict.ddb stardict.txt @@ -11,7 +14,17 @@ concise-enhanced.mdx concise-enhanced.png concise-enhanced.css Secret.py + +# 忽略 output 目录下的所有文件,除了 tag.ddb 和 phonetics.ddb /output/* -/logs/* -/oald-fork/* -qwerty-learner/ +!output/tag.ddb +!output/phonetics.ddb + +# 忽略 logs 目录 +/logs/ + +# 忽略 oald-fork 目录 +/oald-fork/ + +# 忽略 qwerty-learner 目录 +/qwerty-learner/ diff --git a/ddb_to_csv.py b/ddb_to_csv.py new file mode 100644 index 0000000..75004d9 --- /dev/null +++ b/ddb_to_csv.py @@ -0,0 +1,43 @@ +from datetime import datetime +from pathlib import Path + +import duckdb +from loguru import logger + + +def convert_stardictdb_to_csv(stardictdb_file: Path, csv_file: Path) -> None: + """ + 将 stardict.ddb 数据库转换回 stardict.csv 文件。 + + :param stardictdb_file: DuckDB 数据库文件路径 + :param csv_file: 目标 CSV 文件路径 + :raises FileNotFoundError: 如果 stardictdb_file 不存在 + """ + # 检查输入文件是否存在 + if not stardictdb_file.exists(): + logger.error(f"数据库文件 {stardictdb_file} 未找到") + raise FileNotFoundError(f"数据库文件 {stardictdb_file} 未找到") + + # 处理目标文件已存在的情况 + if csv_file.exists(): + current_time = datetime.now().strftime("%Y%m%d_%H%M%S") + new_name = csv_file.with_name(f"stardict_old_{current_time}.csv") + csv_file.rename(new_name) + logger.info(f"已存在的 {csv_file} 已重命名为 {new_name}") + + try: + # 连接到 DuckDB 数据库(只读模式) + with duckdb.connect(database=str(stardictdb_file), read_only=True) as conn: + # 导出数据到 CSV 文件 + conn.execute(f"COPY stardict TO '{csv_file}' (FORMAT CSV, HEADER)") + logger.info(f"成功将 {stardictdb_file} 导出为 {csv_file}") + except Exception as e: + logger.error(f"导出过程中发生错误: {e}") + raise + + +if __name__ == "__main__": + output_dir = Path("output") + stardictdb_file = Path() / output_dir / "stardict.ddb" + stardict_csv = Path() / "stardict.csv" + convert_stardictdb_to_csv(stardictdb_file, stardict_csv) diff --git a/output/phonetics.ddb b/output/phonetics.ddb new file mode 100644 index 0000000..bf453b3 Binary files /dev/null and b/output/phonetics.ddb differ diff --git a/output/tag.ddb b/output/tag.ddb new file mode 100644 index 0000000..edd1b27 Binary files /dev/null and b/output/tag.ddb differ diff --git a/stardict.7z b/stardict.7z index 825ea5d..62cb4bf 100644 Binary files a/stardict.7z and b/stardict.7z differ