71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
"""从 ready_list2_copy1 原样导出,按店名各写一个 Excel。字段不改、不筛列。"""
|
|
import os.path
|
|
import re
|
|
import time
|
|
|
|
import pandas
|
|
import newsetting
|
|
|
|
tent = newsetting.All_Setting('192.168.5.2', 3307, 'mt_newretail')
|
|
|
|
timestamp = time.time()
|
|
local_time = time.localtime(timestamp)
|
|
current_date = time.strftime("%Y-%m-%d", local_time)
|
|
|
|
# workid 日期前缀
|
|
WORKID_PREFIX = '20261105-1'
|
|
TABLE = 'ready_list2_copy1'
|
|
OUT_DIR = os.path.join('./', current_date, 'chengrenyongpin2_按店名')
|
|
|
|
|
|
def sanitize_filename(name):
|
|
name = re.sub(r'[\\/:*?"<>|]', '_', str(name or ''))
|
|
name = name.strip(' .')
|
|
return name or 'unnamed'
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# 先取列名,再按店查全量,原样落盘
|
|
col_sql = f"""
|
|
SELECT COLUMN_NAME
|
|
FROM information_schema.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = '{TABLE}'
|
|
ORDER BY ORDINAL_POSITION
|
|
"""
|
|
col_rows = tent.get_shoppingmalls(col_sql)
|
|
columns = [r[0] for r in col_rows]
|
|
if not columns:
|
|
raise SystemExit(f'[失败] 读不到表结构 {TABLE}')
|
|
|
|
shop_sql = f"""
|
|
SELECT DISTINCT shop_name
|
|
FROM `{TABLE}`
|
|
WHERE workid LIKE '%{WORKID_PREFIX}%'
|
|
"""
|
|
shopname_list = tent.get_shoppingmalls(shop_sql)
|
|
print(f'[开始] 表={TABLE} 前缀={WORKID_PREFIX} 店数={len(shopname_list)}')
|
|
|
|
os.makedirs(OUT_DIR, exist_ok=True)
|
|
col_list = ', '.join(f'`{c}`' for c in columns)
|
|
|
|
for (shop_name,) in shopname_list:
|
|
esc_shop = (shop_name or '').replace("'", "''")
|
|
sql2 = f"""
|
|
SELECT {col_list}
|
|
FROM `{TABLE}`
|
|
WHERE workid LIKE '%{WORKID_PREFIX}%'
|
|
AND shop_name = '{esc_shop}'
|
|
"""
|
|
rows = tent.get_shoppingmalls(sql2)
|
|
if not rows:
|
|
print(f'[跳过] {shop_name} | 无数据')
|
|
continue
|
|
|
|
safe_name = sanitize_filename(shop_name)
|
|
save_path = os.path.join(OUT_DIR, f'{safe_name}.xlsx')
|
|
pa = pandas.DataFrame(rows, columns=columns)
|
|
pa.to_excel(save_path, index=False)
|
|
print(f'[导出] {shop_name} -> {save_path} 行数={len(rows)}')
|
|
|