region_trade.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from decimal import Decimal
  2. import xlrd
  3. # 完整省级行政区划映射(GB/T 2260-2023)
  4. REGION_MAPPING = {
  5. # 直辖市
  6. "北京市": {"code": "110000", "type": "province"},
  7. "天津市": {"code": "120000", "type": "province"},
  8. "上海市": {"code": "310000", "type": "province"},
  9. "重庆市": {"code": "500000", "type": "province"},
  10. # 省份(23个)
  11. "河北省": {"code": "130000", "type": "province"},
  12. "山西省": {"code": "140000", "type": "province"},
  13. "辽宁省": {"code": "210000", "type": "province"},
  14. "吉林省": {"code": "220000", "type": "province"},
  15. "黑龙江省": {"code": "230000", "type": "province"},
  16. "江苏省": {"code": "320000", "type": "province"},
  17. "浙江省": {"code": "330000", "type": "province"},
  18. "安徽省": {"code": "340000", "type": "province"},
  19. "福建省": {"code": "350000", "type": "province"},
  20. "江西省": {"code": "360000", "type": "province"},
  21. "山东省": {"code": "370000", "type": "province"},
  22. "河南省": {"code": "410000", "type": "province"},
  23. "湖北省": {"code": "420000", "type": "province"},
  24. "湖南省": {"code": "430000", "type": "province"},
  25. "广东省": {"code": "440000", "type": "province"},
  26. "海南省": {"code": "460000", "type": "province"},
  27. "四川省": {"code": "510000", "type": "province"},
  28. "贵州省": {"code": "520000", "type": "province"},
  29. "云南省": {"code": "530000", "type": "province"},
  30. "陕西省": {"code": "610000", "type": "province"},
  31. "甘肃省": {"code": "620000", "type": "province"},
  32. "青海省": {"code": "630000", "type": "province"},
  33. "台湾省": {"code": "710000", "type": "province"},
  34. # 自治区(5个)
  35. "内蒙古自治区": {"code": "150000", "type": "province"},
  36. "广西壮族自治区": {"code": "450000", "type": "province"},
  37. "西藏自治区": {"code": "540000", "type": "province"},
  38. "宁夏回族自治区": {"code": "640000", "type": "province"},
  39. "新疆维吾尔自治区": {"code": "650000", "type": "province"},
  40. # 特别行政区
  41. "香港特别行政区": {"code": "810000", "type": "province"},
  42. "澳门特别行政区": {"code": "820000", "type": "province"}
  43. }
  44. def parse_numeric(value):
  45. """增强型数值解析函数(处理空值和特殊符号)[1,8](@ref)"""
  46. try:
  47. cleaned = str(value).strip().replace(',', '')
  48. if cleaned in ('-', '', 'NA', 'NaN'):
  49. return Decimal('0.00')
  50. return Decimal(cleaned).quantize(Decimal('0.00'))
  51. except Exception as e:
  52. print(f"数值解析失败:{value},错误:{str(e)}")
  53. return Decimal('0.00')
  54. def generate_region_sql(input_file, output_file, year_month):
  55. """生成行政区划贸易数据SQL脚本(支持.xls格式)[1,7](@ref)"""
  56. workbook = xlrd.open_workbook(input_file)
  57. sheet = workbook.sheet_by_index(0)
  58. sql_scripts = []
  59. for row_idx in range(6, sheet.nrows):
  60. row = sheet.row_values(row_idx)
  61. region_name = str(row[1]).strip() if len(row) > 1 else ""
  62. if not region_name:
  63. continue
  64. # 处理地区简称(保持原有逻辑)
  65. region_info = REGION_MAPPING.get(region_name) or next(
  66. (v for k, v in REGION_MAPPING.items() if region_name in k), None
  67. )
  68. if not region_info:
  69. print(f"警告:未找到地区编码映射 - {region_name}")
  70. continue
  71. data_fields = {
  72. 'monthly_total': parse_numeric(row[2]),
  73. 'monthly_export': parse_numeric(row[4]),
  74. 'monthly_import': parse_numeric(row[6]),
  75. 'ytd_total': parse_numeric(row[8]),
  76. 'ytd_export': parse_numeric(row[9]),
  77. 'ytd_import': parse_numeric(row[10])
  78. }
  79. # 生成SQL模板[5](@ref)
  80. sql_template = f"""
  81. INSERT INTO `t_yujin_crossborder_region_trade`
  82. (`year_month`, `region_code`, `region_name`, `region_type`,
  83. `monthly_total`, `monthly_import`, `monthly_export`,
  84. `ytd_total`, `ytd_import`, `ytd_export`)
  85. VALUES (
  86. '{year_month}',
  87. '{region_info['code']}',
  88. '{region_name}',
  89. '{region_info['type']}',
  90. {data_fields['monthly_total']},
  91. {data_fields['monthly_import']},
  92. {data_fields['monthly_export']},
  93. {data_fields['ytd_total']},
  94. {data_fields['ytd_import']},
  95. {data_fields['ytd_export']}
  96. ) ON DUPLICATE KEY UPDATE
  97. monthly_total = VALUES(monthly_total),
  98. monthly_import = VALUES(monthly_import),
  99. monthly_export = VALUES(monthly_export),
  100. ytd_total = VALUES(ytd_total),
  101. ytd_import = VALUES(ytd_import),
  102. ytd_export = VALUES(ytd_export);
  103. """
  104. sql_scripts.append(sql_template)
  105. # 写入SQL文件
  106. with open(output_file, 'w', encoding='utf-8') as f:
  107. f.write('\n'.join(sql_scripts))
  108. return len(sql_scripts)
  109. # 执行示例(需提前安装xlrd==1.2.0)
  110. if __name__ == "__main__":
  111. input_excel = r"D:/Downloads/2025051809411226553.xls"
  112. output_sql = "../downloads/region_trade04.sql"
  113. year_month = "2025-04"
  114. try:
  115. count = generate_region_sql(input_excel, output_sql, year_month)
  116. print(f"成功生成{count}条SQL语句,已保存至{output_sql}")
  117. except Exception as e:
  118. print(f"生成失败:{str(e)}")