import re from pathlib import Path import pandas as pd from utils import base_country_code, base_mysql from utils.base_country_code import format_sql_value from utils.log import log CUSTOM_COMMODITY_REPLACEMENTS = { '家具': '家具及其零件', '眼镜': '眼镜及其零件', } # 需要保留中文括号及内容的商品关键词 PRESERVE_PARENTHESES_KEYWORDS = { '汽车(包括底盘)', } def clean_commodity_name(name, preserve_keywords=None): """ 自定义清洗商品名称逻辑,支持条件保留中文括号内容 :param name: 商品名称字符串 :param preserve_keywords: 需要保留括号的关键词集合 :return: 清洗后的商品名称 """ name = str(name).strip() # 去除非必要符号 name = re.sub(r'[#*]', '', name) # 判断是否需要保留括号内容 if preserve_keywords: for keyword in preserve_keywords: if keyword == name: # 匹配到关键词时,不移除括号内容 return name # 默认移除中文括号及内容 name = re.sub(r'([^)]*)', '', name) return name.strip() def process_folder(path): file_paths = list(Path(path).glob('*')) if not file_paths: log.info("未找到任何文件") return year, month = base_country_code.extract_year_month_from_path(path) import_df = pd.DataFrame() export_df = pd.DataFrame() for file in file_paths: file_path = Path(path) / file df = pd.read_excel(file_path, header=None).iloc[6:] value_index = 1 if month == 2 else 3 if "进口商品总值" in file.name: temp_df = df[[0, value_index]].rename(columns={0: 'commodity', value_index: 'import'}) temp_df['commodity'] = ( temp_df['commodity'] .astype(str) .apply(lambda x: clean_commodity_name(x, preserve_keywords=PRESERVE_PARENTHESES_KEYWORDS)) .replace(CUSTOM_COMMODITY_REPLACEMENTS, regex=False) ) temp_df['import'] = pd.to_numeric(temp_df['import'].replace('--', 0), errors='coerce') # 去重 commodity 列,保留第一个出现的行 temp_df = temp_df.drop_duplicates(subset=['commodity'], keep='first') import_df = pd.concat([import_df, temp_df]) elif "出口商品总值" in file.name: temp_df = df[[0, value_index]].rename(columns={0: 'commodity', value_index: 'export'}) temp_df['commodity'] = ( temp_df['commodity'] .astype(str) .apply(lambda x: clean_commodity_name(x, preserve_keywords=PRESERVE_PARENTHESES_KEYWORDS)) .replace(CUSTOM_COMMODITY_REPLACEMENTS, regex=False) ) temp_df['export'] = pd.to_numeric(temp_df['export'].replace('--', 0), errors='coerce') temp_df = temp_df.drop_duplicates(subset=['commodity'], keep='first') export_df = pd.concat([export_df, temp_df]) save_to_database(import_df, export_df, year, month) def save_to_database(import_df, export_df, year, month): # 合并数据(使用outer join保留所有商品) merged_df = pd.merge( import_df.groupby('commodity')['import'].sum().reset_index(), export_df.groupby('commodity')['export'].sum().reset_index(), on='commodity', how='outer' ) year_month = f'{year}-{month:02d}' processed_commodities = set() sql_arr = [] sql_arr_copy = [] try: for _, row in merged_df.iterrows(): commodity_name = str(row['commodity']) if commodity_name == '肉类' or commodity_name == '其他' or commodity_name == '干鲜瓜果' or commodity_name == '钟表': log.info(f'{commodity_name} 商品不存在,跳过') continue commodity_code, commodity_name_fix = base_mysql.get_commodity_id(commodity_name) if not commodity_code: log.info(f"未找到商品名称 '{commodity_name}' 对应的 ID") continue if not commodity_name_fix or commodity_name_fix in processed_commodities: continue monthly_import = round(row['import'] * 10000, 4) monthly_export = round(row['export'] * 10000, 4) monthly_total = round( (0 if pd.isna(monthly_import) else monthly_import) + (0 if pd.isna(monthly_export) else monthly_export), 4 ) if month == 2: year_month_2 = f'{year}-01' monthly_import = round(monthly_import / 2, 4) monthly_export = round(monthly_export / 2, 4) monthly_total = round(monthly_import + monthly_export, 4) sql = (f"INSERT INTO t_yujin_crossborder_prov_commodity_trade " f"(crossborder_year, crossborder_year_month, prov_code, prov_name, commodity_code, commodity_name, monthly_total, monthly_export, monthly_import, create_time) VALUES " f"('{year}', '{year_month_2}', '340000', '安徽省', '{commodity_code}', '{commodity_name_fix}', {format_sql_value(monthly_total)}, {format_sql_value(monthly_export)}, {format_sql_value(monthly_import)}, now());") sql_arr_copy.append(sql) sql = (f"INSERT INTO t_yujin_crossborder_prov_commodity_trade " f"(crossborder_year, crossborder_year_month, prov_code, prov_name, commodity_code, commodity_name, monthly_total, monthly_export, monthly_import, create_time) VALUES " f"('{year}', '{year_month}', '340000', '安徽省', '{commodity_code}', '{commodity_name_fix}', {format_sql_value(monthly_total)}, {format_sql_value(monthly_export)}, {format_sql_value(monthly_import)}, now());") sql_arr.append(sql) processed_commodities.add(commodity_name_fix) # log.info(f'{commodity_name} -> {commodity_name_fix}') except Exception as e: log.info(f"{year_month} prov_commodity_trade 生成 SQL 文件时发生异常: {str(e)}") log.info(f"√ {year_month} prov_commodity_trade 成功生成 SQL 文件 size {len(sql_arr)} ") # 解析完后生成sql文件批量入库 base_mysql.bulk_insert(sql_arr) if month == 2: log.info(f"√ {year_month} prov_commodity_trade copy 成功生成 SQL 文件 size {len(sql_arr_copy)} ") base_mysql.bulk_insert(sql_arr_copy) log.info(f"√ {year_month} prov_commodity_trade SQL 存表完成!\n") def hierarchical_traversal(root_path): """分层遍历:省份->年份->月目录""" root = Path(root_path) # 获取所有年份目录 year_dirs = [ item for item in root.iterdir() if item.is_dir() and base_country_code.YEAR_PATTERN.match(item.name) ] # 按年倒序 for year_dir in sorted(year_dirs, key=lambda x: x.name, reverse=True): # 构造完整的路径:download/shandong/2025/03 log.info(f"\n年份:{year_dir.name} | 省份:anhui") # 提取月份目录 month_dirs = [] for item in year_dir.iterdir(): if item.is_dir() and base_country_code.MONTH_PATTERN.match(item.name): month_dirs.append({ "path": item, "month": int(item.name) }) # 按月倒序输出 if month_dirs: for md in sorted(month_dirs, key=lambda x: x["month"], reverse=True): log.info(f" 月份:{md['month']:02d} | 路径:{md['path']}") process_folder(md['path']) if __name__ == '__main__': hierarchical_traversal(base_country_code.download_dir) # root = Path(base_country_code.download_dir)/'2025'/'04' # process_folder(root) log.info("安徽合肥海关类章所有文件处理完成!")