""" 修復 app_updated.py 中重複的 CSV 路由 """ import re # 讀取檔案 with open('app_updated.py', 'r', encoding='utf-8') as f: content = f.read() # 找到並刪除重複的 CSV 匯入/匯出 API 區塊 (第 852 行開始) # 保留第一次定義(已經移到正確位置的),刪除後面的重複定義 # 找到 "# ==================== CSV 匯入/匯出 API ====================" 的位置 csv_section_pattern = r'# ====================CSV匯入/匯出 API ====================.*?(?=# ====================)' # 刪除重複的 CSV 區塊 (保留第一次定義) lines = content.split('\n') new_lines = [] skip_until_next_section = False first_csv_section_found = False i = 0 while i < len(lines): line = lines[i] # 檢查是否是 CSV 匯入/匯出 API 區段 if '# ==================== CSV 匯入/匯出 API ====================' in line: if not first_csv_section_found: # 第一次遇到,跳過這個區塊(因為我們已經在前面定義了) first_csv_section_found = True skip_until_next_section = True else: # 第二次遇到重複區塊,跳過 skip_until_next_section = True # 檢查是否遇到下一個區段 if skip_until_next_section and '# ====================' in line and 'CSV 匯入/匯出' not in line: skip_until_next_section = False new_lines.append(line) i += 1 continue if not skip_until_next_section: new_lines.append(line) i += 1 # 寫回檔案 with open('app_updated.py', 'w', encoding='utf-8') as f: f.write('\n'.join(new_lines)) print("已修復 CSV 路由重複問題")