Я работаю над проектом, где мне нужно автоматически извлекать данные из файла Excel и загрузить их в базу данных Oracle. Я использую Python, Toad, Oracle Client и VS -код. Цель состоит в том, чтобы запустить загрузку, как только новый файл Excel добавляется в определенную папку. Please assist with a code for this.
import os
import pandas as pd
import cx_Oracle
from time import sleep
oracle_connection_string = 'c##chbz/excelpass@localhost:1521/XE'
#create a connection to the folder where the daily Отчет Excel будет заполнен.
folder_path = r'c: \ users \ userr \ desktop \ forterdailyexcels '< /p>
#concection между Python Script и Oracle Database
def connect_to_oracle ():
try:
connect /> print ("успешно подключен:")
return Connection
кроме cx_oracle.databaseError как e:
print ("Была ошибка, соединяющая с базой данных:", e)
network none
connect_to_oracle () < /p>
#create aement a Excel heal /> try:
data = pd.read_excel (file_path)
return data
за исключением исключения как e:
print (f "ошибка чтения файла Excel: {e}")
nether none < /p>
Теперь назовите функцию как: < /h1>
file_path = r'c: \ users \ userr \ desktop \ folderdailyexcels \ excel1.xlsx '
read_excel (file_path) < /p>
#upload data к базе данных oracle
def upload_to_oracle (data_table):
connection = connect_to_to_oracle ()
вернуть < /p>
cursor = connection.cursor()
for index, row in data.iterrows():
try:
sql = f"INSERT INTO {Project_table} (FULLNAME, NOOFATTACK, DATEOFATTACK) VALUES (:1, :2, :3)"
cursor.execute(sql, (row['column1'], row['column2'], row['column3']))
connection.commit()
except Exception as e:
print(f"Error uploading row {index}: {e}")
cursor.close()
connection.close()
< /code>
#Monitor Папка и автоматизация ETL
def monitorer_folder ():
while true:
files = os.listdir (folder_path)
для файлов в файлах:
if file.endswith (". xlsx"):
file_path = os.path. /> print (f "Нашел новый файл: {file_path}")
data = read_excel (file_path)
Если данные не являются:
opload_to_oracle (data, 'project_table') # изменить 'project_table>
Подробнее здесь: https://stackoverflow.com/questions/795 ... ined-table
ETL из Excel File в базу данных Oracle с той же определенной таблицей ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение