신호생성 repo (24. 1. 5 ~).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

175 lines
4.7 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import pyodbc, os, json, csv\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'connection_info': {'DSNNAME': 'Tibero6',\n",
" 'DBUSER': 'snits',\n",
" 'DBPWD': 'snitsUIspxmworks#PW'},\n",
" 'paths': {'data': ['Data'],\n",
" 'intermediates': ['Intermediates'],\n",
" 'results': ['Results'],\n",
" 'tables': ['Data', 'tables'],\n",
" 'networks': ['Data', 'networks'],\n",
" 'scripts': ['Scripts']}}"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"c:\\Github\\snits_siggen\\Data\\tables\n"
]
}
],
"source": [
"# 루트폴더 지정\n",
"path_root = os.path.dirname(os.path.dirname(os.path.abspath('.')))\n",
"with open(os.path.join(path_root, 'configs', 'config_revised.json'), 'r') as config_file:\n",
" config = json.load(config_file)\n",
"\n",
"display(config)\n",
"\n",
"starting_time = datetime.now()\n",
"\n",
"# 주요 폴더 경로 지정\n",
"paths = config['paths']\n",
"path_tables = os.path.join(path_root, *paths['tables'])\n",
"# path_results = os.path.join(path_root, *paths['results'])\n",
"\n",
"print(path_tables)\n",
"# print(path_results)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"connection_info = config['connection_info']\n",
"DSNNAME = connection_info[\"DSNNAME\"]\n",
"DBUSER = connection_info[\"DBUSER\"]\n",
"DBPWD = connection_info[\"DBPWD\"]\n",
"cnxn = pyodbc.connect(f'DSN={DSNNAME};UID={DBUSER};PWD={DBPWD};charset=utf-8')\n",
"cursor = cnxn.cursor()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# 스키마 및 테이블명\n",
"schema = 'SNMS'\n",
"tables = ['TC_IF_TOD_DAY_PLAN', # TOD 일계획 현황#\n",
" 'TC_IF_TOD_HOLIDAY_PLAN', # TOD 휴일 계획#\n",
" 'TC_IF_TOD_RED_YELLO', # TOD 적색 및 황색 시간#\n",
" 'TC_IF_TOD_WEEK_PLAN', # TOD 주 계획#\n",
" # 'TL_IF_SIGL', # 신호 운영 이력\n",
" 'TL_IF_SIGL_CYCL', # 신호 운영 주기 이력#\n",
" 'TM_FA_CRSRD', # 교차로 마스터#\n",
" 'TN_IF_SIGL_FLOW', # 신호 현시 별 이동류 방향#\n",
" ]\n",
"\n",
"# 폴더 Data\\tables\\yyyymmdd_hhmmss 생성\n",
"timestamp = starting_time.strftime('%Y%m%d')\n",
"# base_dir = os.path.join(path_tables, timestamp)\n",
"os.makedirs(os.path.join(path_tables, timestamp), exist_ok=True)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def fetch_table(table, condition=\"\"):\n",
" try:\n",
" query = f\"SELECT * FROM {schema}.{table} {condition}\"\n",
" cursor.execute(query)\n",
" csv_file_path = os.path.join(path_tables, timestamp, f\"{table}.csv\")\n",
" with open(csv_file_path, 'w', newline='', encoding='utf-8-sig') as csvfile:\n",
" csv_writer = csv.writer(csvfile)\n",
" columns = [column[0] for column in cursor.description]\n",
" csv_writer.writerow(columns)\n",
" for row in cursor.fetchall():\n",
" csv_writer.writerow(row)\n",
" except Exception as e:\n",
" print(f\"오류 발생: {e}\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TC_IF_TOD_DAY_PLAN\n",
"TC_IF_TOD_HOLIDAY_PLAN\n",
"TC_IF_TOD_RED_YELLO\n",
"TC_IF_TOD_WEEK_PLAN\n",
"TL_IF_SIGL_CYCL\n",
"TM_FA_CRSRD\n",
"TN_IF_SIGL_FLOW\n"
]
}
],
"source": [
"for table in tables:\n",
" print(table)\n",
" fetch_table(table)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "ta",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}