2017-03-01 20 views
0

我有一個Excel電子表格,我想從它導出基本數據到我的Oracle表中。索引DataFrame值,直到達到值/字符串

雖然Excel表格在某些單元格中有額外的不必要的數據。它也分裂中途,表明可用的魚和所需的魚之間的差異。我需要捕捉這種差異。

所以我的目標是導出所有的數據,但在我的Oracle數據庫中,我需要區分提供的魚和期望的魚。那麼有沒有辦法索引列,直到索引達到QUOTA TO BUY?因此,允許我在ask列中導出第一個塊,其中包含1,並在我的bid列中導出第二個塊與1

我到目前爲止試過的代碼如下,以及Excel表格的圖片。謝謝你的幫助。

enter image description here

import os 
import numpy as np 
import pandas as pd 
import cx_Oracle 
import re 
from dateutil import parser 

dsnStr = cx_Oracle.makedsn("sole.noaa.gov", "1526", "sole") 
con = cx_Oracle.connect(user="user", password="passsword", dsn=dsnStr) 

path = 'Z:\\excel_file_to_convert' 

#pattern = re.compile(r'Sent:(.+?)(?=<br/>)') 

for filename in os.listdir(path): 
    file_path = os.path.join(path, filename) 
    if os.path.isfile(file_path): 
     df = pd.read_excel(file_path) 
     print("df is:", df) 
     print("column 1 I think:", df[:DESIRED STOCK]) 
     print("row 1:", df.loc[0]) 
     print("row 2:", df.loc[1]) 
     print("row 3:", df.loc[2]) 
     print("row 4:", df.loc[3]) 
     print("row 5:", df.loc[4]) 

     #d = parser.parse(df, fuzzy=True) 
     #print(d)   

     #df['DATE'] = pd.to_datetime(df['DATE'])   # convert date column to datetimes 
     #latest_date = df['DATE'].max()     # find the latest datetime 
     #latest_rows = df[df['DATE'] == latest_date]  # use index filtering to choose only columns equal to latest date 
     #print ("latest_rows is:", latest_rows) 



cursor = con.cursor() 
exported_data = [tuple(x) for x in df.values] 
sql_query = ("INSERT INTO ROUGHTABLE(species, date_posted, stock_id, pounds, money, sector, ask)" "VALUES(:3, :1, :2, :4, :5, 'Sustainable Harvest Sector', '1')") 

#sql_query = ("INSERT INTO DATABASE(species, trade_date, trade_id, pounds, advertised_price, email_year, email_month, email_day, sector, ask)" "VALUES(:3, :1, :2, :4, :5, :6, :7, :8, 'Sustainable Harvest Sector', '1')") 
cursor.executemany(sql_query, exported_data) 
con.commit() #commit to database 

cursor.close() 
con.close() 

回答

0

除非你需要重複做,那麼你並不需要使用額外的語言。

將其粘貼到單元格F4中,並將其複製到要插入第一個塊的所有行中。

="INSERT INTO ROUGHTABLE(date_posted, stock_id, species, pounds, money, sector, ask) VALUES (DATE '"&YEAR(A4)&"-"&MONTH(A4)&"-"&DAY(A4)&"', "&B4&", '"&C4&"', '"&D4&"', "&E4&", 'Sustainable Harvest Sector', '1');" 

將生成的SQL語句複製並粘貼到腳本中並在SQL/Plus中運行它。

然後,您可以編輯第二個塊的公式並根據需要重複該過程。

+0

雖然....確實需要重複執行此操作,這是否意味着這些SQL代碼在其他Excel工作表上不起作用? – theprowler

+0

如果它們的格式完全相同,則可以將其複製並粘貼到多個電子表格中。只需編輯需要編輯的內容(如「扇區」或「詢問」/「出價」列),以便每張表格均正確。如果你只是爲少數幾張紙做這件事,那麼它會起作用,但如果你每週都這樣做,或者每週都這樣做,那麼花時間編寫一個像你開始的程序化解決方案可能是值得的。 – MT0

+0

嗯好的。我必須做大約120次,然後每週一次。所以我想最好是我想讓程序去做。老實說,大多數電子表格並沒有在'Available'和'Desired'之間進行分割,所以我只需要手動輸入這個。但是現在我遇到的問題是我需要那個始終在第一個單元格中出現'LISTING'的日期,所以我認爲我將RegEx用於它,但是我對RegEx不太好,所以這需要一段時間。然後,我需要解決捕獲所有基本數據,但擺脫前兩個標題單元格 – theprowler