|
|
@@ -104,7 +104,11 @@ def add_search_links(data, column_index, start_row=0):
|
|
|
def save_csv(data, file_path, encoding='utf-8-sig'):
|
|
|
"""将数据保存为CSV文件"""
|
|
|
try:
|
|
|
- with open(file_path, mode='w', newline='', encoding=encoding) as file:
|
|
|
+ # 确保使用能够处理所有字符的编码
|
|
|
+ if encoding.lower() in ['gb2312', 'gbk']:
|
|
|
+ encoding = 'gb18030' # 更全面的中文编码
|
|
|
+
|
|
|
+ with open(file_path, mode='w', newline='', encoding=encoding, errors='replace') as file:
|
|
|
writer = csv.writer(file)
|
|
|
writer.writerows(data)
|
|
|
except Exception as e:
|
|
|
@@ -127,14 +131,14 @@ def process_csv(input_file, output_file, column_index, start_row=0, target_langu
|
|
|
# 为搜索词添加超链接
|
|
|
data = add_search_links(data, column_index, start_row)
|
|
|
|
|
|
- # 保存为新文件,使用检测到的编码或默认utf-8-sig
|
|
|
- save_csv(data, output_file, encoding=detected_encoding or 'utf-8-sig')
|
|
|
+ # 保存为新文件,强制使用utf-8-sig编码以确保兼容性
|
|
|
+ save_csv(data, output_file, encoding='utf-8-sig')
|
|
|
|
|
|
print(f"Successfully processed and saved to {output_file}")
|
|
|
|
|
|
except Exception as e:
|
|
|
print(f"Error processing CSV file: {e}")
|
|
|
- raise
|
|
|
+ exit(1) # 遇到错误立即退出
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
input_file = "测试.csv"
|