Spaces:
Sleeping
Sleeping
Download import_to_supabase.py from zbgool/quant-engine: direct link, hf CLI and curl.
- Browser
- Download file 2.21 kB
-
https://huggingface.co/spaces/zbgool/quant-engine/resolve/main/import_to_supabase.py
- Command line
-
hf download hf://spaces/zbgool/quant-engine/import_to_supabase.py
-
curl -L -o import_to_supabase.py https://huggingface.co/spaces/zbgool/quant-engine/resolve/main/import_to_supabase.py
2.21 kB
| """ | |
| 将本地 .day 文件导入 Supabase stock_daily_prices 表 | |
| 用法: python import_to_supabase.py [数量限制] | |
| """ | |
| import sys | |
| import os | |
| import glob | |
| sys.path.insert(0, os.path.dirname(__file__)) | |
| import config | |
| from data_loader import load_day_file | |
| from supabase import create_client | |
| def import_day_files(data_dir: str, limit: int = 10): | |
| client = create_client(config.SUPABASE_URL, config.SUPABASE_KEY) | |
| # 收集所有 .day 文件(sh/lday + sz/lday) | |
| patterns = [ | |
| os.path.join(data_dir, "sh", "lday", "*.day"), | |
| os.path.join(data_dir, "sz", "lday", "*.day"), | |
| ] | |
| all_files = [] | |
| for p in patterns: | |
| all_files.extend(glob.glob(p)) | |
| all_files = all_files[:limit] | |
| print(f"共找到文件,取前 {len(all_files)} 个导入") | |
| total_rows = 0 | |
| for filepath in all_files: | |
| fname = os.path.basename(filepath) | |
| code = fname[:-4] # e.g. sh000001 | |
| df = load_day_file(filepath) | |
| if df.empty: | |
| print(f" {code}: 空文件,跳过") | |
| continue | |
| # 转为 Supabase 可插入的 dict 列表 | |
| records = [] | |
| for date, row in df.iterrows(): | |
| records.append({ | |
| "code": code, | |
| "date": date.strftime("%Y-%m-%d"), | |
| "open": round(row["open"], 4), | |
| "high": round(row["high"], 4), | |
| "low": round(row["low"], 4), | |
| "close": round(row["close"], 4), | |
| "amount": round(row["amount"], 2), | |
| "volume": round(row["volume"], 2), | |
| }) | |
| # 分批插入(Supabase 单次建议不超过 1000 行) | |
| batch_size = 500 | |
| for i in range(0, len(records), batch_size): | |
| batch = records[i:i + batch_size] | |
| client.table("stock_daily_prices").upsert( | |
| batch, on_conflict="code,date" | |
| ).execute() | |
| total_rows += len(records) | |
| print(f" {code}: {len(records)} 行已导入") | |
| print(f"\n导入完成!共 {total_rows} 行") | |
| if __name__ == "__main__": | |
| limit = int(sys.argv[1]) if len(sys.argv) > 1 else 10 | |
| data_dir = "D:/new_tdx2/vipdoc" | |
| import_day_files(data_dir, limit) | |