# -*- coding: utf-8 -*-
import csv
import json
CSV_PATH = "raw.csv"
NOT_FOUND_URL = "https://leetcode.cn/problems/404-not-found-problem/"
def parse_csv(file_path):
problems = []
with open(file_path, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
problems.append({
"code": row["题号"],
"name": row["题目名"].strip(),
"difficulty": row["难度"].strip(),
"freq": int(row["次数"]),
"company": [c.strip() for c in row["公司"].split(",") if c.strip()],
"url": row["链接"].strip() or NOT_FOUND_URL
})
return problems
def to_markdown(problems):
print("\n📄 Markdown 表格格式:\n")
print("| 题号 | 题目名 | 难度 | 次数 | 公司 |")
print("|------|--------|------|------|------|")
for p in problems:
title_link = f"[{p['name']}]({p['url']})" if p['url'] else p['name']
companies = ", ".join(p['company'])
print(f"| {p['code']} | {title_link} | {p['difficulty']} | {p['freq']} | {companies} |")
print()
def to_json(problems):
for p in problems:
if p["difficulty"] == "简单":
p["difficulty"] = "Easy"
elif p["difficulty"] == "中等":
p["difficulty"] = "Medium"
elif p["difficulty"] == "困难":
p["difficulty"] = "Hard"
print("\n🧩 JSON 格式(JavaScript 结构):\n")
print(json.dumps(problems, ensure_ascii=False, indent=2))
print()
def main():
problems = parse_csv(CSV_PATH)
print("📚 已加载题目数:", len(problems))
to_json(problems)
# while True:
# print("\n请选择输出格式:")
# print("1. Markdown 表格格式")
# print("2. JavaScript JSON 数组")
# print("0. 退出")
# choice = input("你的选择是(输入数字):").strip()
# if choice == "1":
# to_markdown(problems)
# elif choice == "2":
# to_json(problems)
# elif choice == "0":
# print("👋 再见!")
# break
# else:
# print("⚠️ 无效选项,请输入 1 / 2 / 0")
if __name__ == "__main__":
main()