|
@@ -25,10 +25,11 @@ def index_to_column_letter(index: int) -> str:
|
|
|
|
|
|
|
|
def read_csv_with_header(
|
|
def read_csv_with_header(
|
|
|
file_path: str,
|
|
file_path: str,
|
|
|
- encoding: str = 'cp936'
|
|
|
|
|
|
|
+ encoding: str = 'utf-8'
|
|
|
) -> List[List[str]]:
|
|
) -> List[List[str]]:
|
|
|
"""读取CSV文件并返回数据和表头"""
|
|
"""读取CSV文件并返回数据和表头"""
|
|
|
try:
|
|
try:
|
|
|
|
|
+ logger.debug(f"encoding = {encoding}")
|
|
|
with open(file_path, 'r', encoding=encoding) as f:
|
|
with open(file_path, 'r', encoding=encoding) as f:
|
|
|
reader = csv.reader(f)
|
|
reader = csv.reader(f)
|
|
|
header = next(reader) # 读取表头(第1行)
|
|
header = next(reader) # 读取表头(第1行)
|
|
@@ -133,7 +134,7 @@ def process_csv(
|
|
|
end_row: Optional[int] = None,
|
|
end_row: Optional[int] = None,
|
|
|
source_lang: str = 'auto',
|
|
source_lang: str = 'auto',
|
|
|
target_lang: str = 'zh-CN',
|
|
target_lang: str = 'zh-CN',
|
|
|
- encoding: str = 'cp936'
|
|
|
|
|
|
|
+ encoding: str = 'utf-8'
|
|
|
):
|
|
):
|
|
|
"""处理CSV文件的主函数"""
|
|
"""处理CSV文件的主函数"""
|
|
|
try:
|
|
try:
|
|
@@ -168,11 +169,11 @@ if __name__ == "__main__":
|
|
|
load_dotenv()
|
|
load_dotenv()
|
|
|
|
|
|
|
|
# 示例用法
|
|
# 示例用法
|
|
|
- file_path = "/home/mrh/code/excel_tool/temp/测试.csv"
|
|
|
|
|
|
|
+ file_path = "/home/mrh/code/excel_tool/temp/测试_url_processed.csv"
|
|
|
output_path = "/home/mrh/code/excel_tool/temp/测试_processed.csv"
|
|
output_path = "/home/mrh/code/excel_tool/temp/测试_processed.csv"
|
|
|
|
|
|
|
|
# 读取文件并搜索关键词
|
|
# 读取文件并搜索关键词
|
|
|
- header, data = read_csv_with_header(file_path)
|
|
|
|
|
|
|
+ header, data = read_csv_with_header(file_path, encoding='utf-8')
|
|
|
found_columns = search_keywords(data, header, ["搜索词", "类别"])
|
|
found_columns = search_keywords(data, header, ["搜索词", "类别"])
|
|
|
|
|
|
|
|
# 处理文件
|
|
# 处理文件
|