Explorar el Código

feat: improve error handling and debugging in translation process

Your Name (aider) hace 1 año
padre
commit
cb7c9d6098
Se han modificado 1 ficheros con 18 adiciones y 4 borrados
  1. 18 4
      process_data.py

+ 18 - 4
process_data.py

@@ -34,11 +34,20 @@ def process_row(row, search_term_index):
     # Add translation column after search term
     search_term = row[search_term_index]
     try:
+        print(f"Translating: {search_term}")
         translations = translate_sentences([search_term])
-        translated = translations[0] if translations else "翻译失败"
+        print(f"Translation result: {translations}")
+        
+        if not translations or len(translations) == 0:
+            translated = "翻译失败(无结果)"
+        elif isinstance(translations, int):  # Handle case where function returns error code
+            translated = f"翻译失败(错误码:{translations})"
+        else:
+            translated = translations[0]
+            
     except Exception as e:
-        print(f"Translation error for '{search_term}': {e}")
-        translated = "翻译失败"
+        print(f"Translation error for '{search_term}': {str(e)}")
+        translated = f"翻译失败(异常:{str(e)})"
     
     row.insert(search_term_index + 1, translated)
     
@@ -62,9 +71,14 @@ def main(input_file, output_file):
         search_term_index = 1  # Search term is in second column
         for i, row in enumerate(data[1:], start=1):
             try:
+                print(f"\nProcessing row {i}")
                 data[i] = process_row(row, search_term_index)
+                print(f"Processed row {i} successfully")
             except Exception as e:
-                print(f"Error processing row {i}: {e}")
+                print(f"Error processing row {i}: {str(e)}")
+                # Insert empty translation column to maintain structure
+                row.insert(search_term_index + 1, "翻译失败(处理错误)")
+                data[i] = row
                 continue
         
         # Save processed data