{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pyodbc, os, tqdm, json, csv\n", "import pandas as pd\n", "from datetime import datetime" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'c:\\\\Github\\\\snits_siggen\\\\configs\\\\config.json'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[2], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# 루트폴더 지정\u001b[39;00m\n\u001b[0;32m 2\u001b[0m path_root \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mabspath(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m'\u001b[39m)))\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpath_root\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mconfigs\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mconfig.json\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m config_file:\n\u001b[0;32m 4\u001b[0m config \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mload(config_file)\n\u001b[0;32m 6\u001b[0m display(config)\n", "File \u001b[1;32mc:\\Github\\snits_siggen\\siggen\\lib\\site-packages\\IPython\\core\\interactiveshell.py:284\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m 277\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m 278\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 279\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 280\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 281\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 282\u001b[0m )\n\u001b[1;32m--> 284\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'c:\\\\Github\\\\snits_siggen\\\\configs\\\\config.json'" ] } ], "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": null, "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": null, "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": null, "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": null, "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 }