步骤
- 1、Mysql 拉取数据
- 2、封装data Frame类型
- 3、转csv 文件输出 ( excel 打开中文乱码问题)
import logging
from tool.db_client.mysql_client.mysql import MySqlClient
from config.file_conf import BASE_DATA_DIR
from tool.file_util import makedir
import time,datetime
import pandas as pd
logger = logging.getLogger()
def downLoad_rt_rental_shop_cluster_snapshot():
find_time = (datetime.datetime.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d 23:00:00')
SQL_SHOP_CLUSTER_SNAPSHOT = """
SELECT cluster_id,
name,
landmark_name,
famous_area_name,
city_id,
city_name,
c_longitude,
c_latitude,
rental_shop_count,
lasthour_usable_vehicle_count,
usable_vehicle_count,
pick_vehicle_count,
return_vehicle_count,
pick_forecast_count,
dt,
capture_time
FROM rt_rental_shop_cluster_snapshot
WHERE city_id = "1771"
and concat(substr(capture_time , 1,4),"-",substr(capture_time,5,2) ,"-", substr(capture_time,7,2)," ", "23:00:00") <= "%s"
and concat(substr(capture_time , 1,4),"-",substr(capture_time,5,2) ,"-", substr(capture_time,7,2)," ", "23:00:00") >= DATE_SUB("%s",INTERVAL 35 day)
"""
find_time = (datetime.datetime.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d 23:00:00')
SQL_SHOP_CLUSTER_SNAPSHOT %= (find_time, find_time)
ms = MySqlClient()
conn, cursor = ms.get_conn_cursor('idp')
start_time = time.time()
records = ms.execute_sql(conn, cursor, SQL_SHOP_CLUSTER_SNAPSHOT)
ms.close(conn, cursor)
makedir(BASE_DATA_DIR + "\\" + "rt_rental_shop_cluster_snapshot")
dowload_file_name = "rt_rental_shop_cluster_snapshot_" + get_yesterday_str() + ".csv"
file_path = BASE_DATA_DIR + "\\" + "rt_rental_shop_cluster_snapshot" + "\\" + dowload_file_name
cols = ["cluster_id","name","landmark_name","famous_area_name","city_id",
"city_name","c_longitude","c_latitude","rental_shop_count","lasthour_usable_vehicle_count",
"usable_vehicle_count","pick_vehicle_count", "return_vehicle_count","pick_forecast_count","dt",
"capture_time"
]
pd.DataFrame(np.array(records),columns=cols).to_csv(file_path, encoding="utf_8_sig", index=False)
logger.info('rt_rental_shop_cluster_snapshot 数据, 下载耗时 %s 秒' % (round(time.time() - start_time, 0)))