monthData2023.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import re
  2. from decimal import Decimal, InvalidOperation
  3. import xlrd
  4. import pymysql
  5. from datetime import datetime
  6. # 数据库配置(需根据实际情况修改)
  7. DB_CONFIG = {
  8. 'host': '10.130.75.149',
  9. 'port': 3307,
  10. 'user': 'yto_crm',
  11. 'password': '%3sFUlsolaRI',
  12. 'database': 'crm_uat',
  13. 'charset': 'utf8mb4'
  14. }
  15. def convert_unit(value):
  16. """亿元转万元,处理空值"""
  17. try:
  18. # 如果 value 不是特殊的无效值,进行转换并保留4位小数
  19. return round(Decimal(value) * 10000, 4) if value not in ['-', ''] else None
  20. except (InvalidOperation, ValueError):
  21. # 捕获异常,返回 None
  22. return None
  23. def get_upsert_sql():
  24. """生成带覆盖更新的SQL模板"""
  25. return """
  26. INSERT INTO `t_yujin_crossborder_monthly_summary`
  27. (`year_month`, `monthly_total`, `monthly_import`, `monthly_export`, `trade_balance`,
  28. `ytd_total`, `ytd_import`, `ytd_export`, `ytd_trade_balance`, `create_time`)
  29. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  30. ON DUPLICATE KEY UPDATE
  31. monthly_total = VALUES(monthly_total),
  32. monthly_import = VALUES(monthly_import),
  33. monthly_export = VALUES(monthly_export),
  34. trade_balance = VALUES(trade_balance),
  35. ytd_total = VALUES(ytd_total),
  36. ytd_import = VALUES(ytd_import),
  37. ytd_export = VALUES(ytd_export),
  38. ytd_trade_balance = VALUES(ytd_trade_balance),
  39. create_time = VALUES(create_time)
  40. """
  41. def main():
  42. # 连接数据库[2,5](@ref)
  43. try:
  44. conn = pymysql.connect(**DB_CONFIG)
  45. cursor = conn.cursor()
  46. except pymysql.Error as e:
  47. print(f"数据库连接失败: {e}")
  48. return
  49. # 读取Excel文件
  50. try:
  51. workbook = xlrd.open_workbook('../src/downloads/20250513/2025011810224811354 (1).xls')
  52. sheet = workbook.sheet_by_index(0)
  53. except Exception as e:
  54. print(f"文件读取失败: {e}")
  55. conn.close()
  56. return
  57. sql = get_upsert_sql()
  58. current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  59. processed = 0
  60. # 数据遍历(从第6行开始)
  61. for row_idx in range(5, sheet.nrows):
  62. try:
  63. base_row = sheet.row_values(row_idx)
  64. date_cell = str(base_row[1])
  65. # 验证2023年数据格式[6](@ref)
  66. if not re.match(r"^2023\.\d{2}$", date_cell):
  67. continue
  68. # 数据转换
  69. params = (
  70. base_row[1].replace('.', '-'), # year_month
  71. convert_unit(base_row[2]), # monthly_total
  72. convert_unit(base_row[4]), # monthly_import
  73. convert_unit(base_row[3]), # monthly_export
  74. convert_unit(base_row[5]), # trade_balance
  75. convert_unit(base_row[6]), # ytd_total
  76. convert_unit(base_row[7]), # ytd_import
  77. convert_unit(base_row[8]), # ytd_export
  78. convert_unit(base_row[9]), # ytd_balance
  79. current_time # create_time
  80. )
  81. # 执行覆盖插入[6,7](@ref)
  82. cursor.execute(sql, params)
  83. processed += 1
  84. except Exception as e:
  85. print(f"处理行{row_idx}出错: {str(e)}")
  86. conn.rollback()
  87. # 提交事务
  88. try:
  89. conn.commit()
  90. print(f"成功处理 {processed} 条数据,时间:{current_time}")
  91. except pymysql.Error as e:
  92. print(f"事务提交失败: {e}")
  93. finally:
  94. cursor.close()
  95. conn.close()
  96. if __name__ == "__main__":
  97. main()