借车宝数据链接空值处理更新

This commit is contained in:
z66
2025-09-18 10:22:52 +08:00
parent a33518e078
commit 30cacc7da2
6 changed files with 160 additions and 21 deletions
+101
View File
@@ -0,0 +1,101 @@
{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-09-15T06:38:10.678825Z",
"start_time": "2025-09-15T06:38:10.523582Z"
}
},
"source": [
"from datetime import datetime, timezone, timedelta, date, UTC\n",
"import holidays\n",
"from config import Config\n",
"import psycopg2\n",
"import pandas as pd\n",
"import pymysql\n",
"from api import API\n",
"from log_config import configure_task_logger, configure_error_task_logger\n",
"\n",
"\n",
"def get_jcb_details():\n",
" \"\"\"\n",
" 从固定的数据库中获取前几天的NGV明细。\n",
" 参数 `days_back` 表示相对于今天的天数偏移量,默认为1(即前一天)。\n",
" 返回包含NGV明细的pandas DataFrame。\n",
" \"\"\"\n",
" # 保存为CSV文件\n",
" output_dir = \"output\" # 设置输出目录\n",
"\n",
" # 创建输出目录(如果不存在)\n",
" import os\n",
" os.makedirs(output_dir, exist_ok=True)\n",
"\n",
" try:\n",
" # 获得连接并创建游标\n",
" conn = pymysql.connect(\n",
" host=Config.BI_CONN_host,\n",
" database=Config.BI_CONN_INFO_database,\n",
" user=Config.BI_CONN_INFO_user,\n",
" password=Config.BI_CONN_INFO_password,\n",
" # charset='utf8mb4', # 设置字符集以避免编码问题\n",
" # cursorclass=pymysql.cursors.DictCursor # 返回字典形式的结果\n",
" )\n",
" cursor = conn.cursor()\n",
"\n",
" # 获取指定天数前的日期\n",
" # now_time = datetime.now()\n",
" # target_time = now_time + timedelta(days=-days_back)\n",
" target_date_id = \"接车宝\" # 获取目标日期\n",
"\n",
" # SQL 查询语句\n",
" sql = f\"\"\"\n",
" SELECT * FROM jdy_hs_holo_dws_sales_magic_box_ngv_d;\n",
" \"\"\"\n",
"\n",
" # 执行查询并获取结果\n",
" cursor.execute(sql)\n",
" rows = cursor.fetchall() # pymysql 的 DictCursor 会返回字典列表\n",
" print(rows)\n",
" except:\n",
" pass\n",
"\n",
"get_jcb_details()"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"()\n"
]
}
],
"execution_count": 5
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}