|
|
@@ -1,5 +1,6 @@
|
|
|
import csv
|
|
|
from urllib.parse import quote
|
|
|
+from pathlib import Path
|
|
|
|
|
|
def create_hyperlink(value, base_url):
|
|
|
"""为给定的值创建亚马逊搜索页面的超链接"""
|
|
|
@@ -29,6 +30,10 @@ def column_letter_to_index(col_letter):
|
|
|
|
|
|
def read_and_process_csv(input_file, output_file, brand_columns, asin_columns, use_letters=False, base_url='', start_row=0):
|
|
|
"""读取CSV文件并处理指定列的数据,然后写入新文件"""
|
|
|
+ # 确保输出目录存在
|
|
|
+ output_dir = Path(output_file).parent
|
|
|
+ output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
+
|
|
|
with open(input_file, 'r', encoding='utf-8', errors='ignore') as csvfile, \
|
|
|
open(output_file, 'w', newline='', encoding='utf-8') as new_csvfile:
|
|
|
|
|
|
@@ -56,8 +61,9 @@ def read_and_process_csv(input_file, output_file, brand_columns, asin_columns, u
|
|
|
writer.writerow(processed_row)
|
|
|
|
|
|
# 定义要处理的CSV文件路径、输出文件路径以及需要添加超链接的列索引或列字母和亚马逊搜索的基础URL
|
|
|
-input_csv_path = '测试.csv'
|
|
|
-output_csv_path = '测试_processed.csv'
|
|
|
+output_dir = Path('temp')
|
|
|
+input_csv_path = output_dir / '测试_utf8.csv'
|
|
|
+output_csv_path = output_dir / '测试_processed.csv'
|
|
|
brand_columns = ['C', 'D', 'E'] # 品牌列
|
|
|
asin_columns = ['H', 'L', 'P'] # ASIN列
|
|
|
use_letters = True # 是否使用列字母代替索引
|