【发布时间】:2017-07-21 00:41:11
【问题描述】:
我有一个 Excel 电子表格,我想将其中的基本数据导出到我的 Oracle 表中。
不过,Excel 工作表在某些单元格中有多余的不必要数据。它还会在中途分裂,表明可用鱼和所需鱼之间的差异。我需要捕捉这种差异。
所以我的目标是导出所有数据,但在我的 Oracle 数据库中,我需要区分提供的鱼和想要的鱼。那么有没有办法对列进行索引,直到索引达到QUOTA TO BUY?因此允许我在我的ask 列中导出带有1 的第一个块,并在我的bid 列中导出带有1 的第二个块。
到目前为止我尝试过的代码如下,以及 Excel 表格的图片。感谢您的帮助。
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()
【问题讨论】: