我无法弄清楚这个错误意味着什么,因为这个问题在网上已经没有多少帮助了。
是不是说我的Oracle表只需要接收数字而不是字母?情况可能并非如此,因为我将我的列设置为允许VarChar
,以便数字和字母都适合。
我原以为是因为我的DataFrame中有NaN
s,所以我用0&替换了所有这些,我仍然收到此错误。
最后,我想将我从Excel文件中解析的所有数据导出到我的Oracle表中。
任何帮助解决这个问题都将受到赞赏。
我的DataFrame:
S USTAINABLE H ARVEST S ECTOR| QUOTA LISTING JUN 11 2013 Unnamed: 1 \
0 AVAILABLE QUOTA 0
1 DATE TRADE ID
2 6/4/13 130196
3 5/28/13 130158
4 6/4/13 130210
5 5/14/13 130079
6 6/4/13 130187
7 6/4/13 130208
8 6/11/13 130249
9 6/4/13 130204
10 5/28/13 130148
11 5/28/13 130149
12 5/28/13 130157
13 5/21/13 130105
14 5/21/13 130106
15 6/4/13 130205
16 6/11/13 130250
17 6/4/13 130206
18 6/11/13 130248
19 QUOTA TO BUY 0
20 DATE TRADE ID
21 6/11/13 130239
22 5/14/13 130074
23 3/26/13 130006
24 5/14/13 130075
25 5/7/13 130023
26 5/14/13 130039
27 1 0
Unnamed: 2 Unnamed: 3 Unnamed: 4
0 0 0 0
1 AVAILABLE STOCK AMOUNT SALE PRICE
2 COD GBW 10000 0.6
3 COD GBW 300 0.6
4 HADDOCK GBE UP TO 30,000 OFFERS
5 PLAICE 1000 0.45
6 WHITE HAKE UP TO 25,000 0.5
7 WHITE HAKE 4000 0.5
8 WINTER GB 3300 0.25
9 WINTER GB 10000 0.48
10 WINTER GB 1U0P 0T,0O00 0.25
11 WINTER GB UP TO 10,000 0.4
12 WINTER GB 1400 0.25
13 WINTER GB 10000 0.5
14 WINTER GB 10000 0.5
15 WINTER GOM 1000 0.38
16 WINTER SNE 6500 0.4
17 WINTER SNE 3000 0.63
18 YELLOWTAIL GOM 2000 1.25
19 0 0 0
20 DESIRED STOCK AMOUNT BUY PRICE
21 COD GOM UP TO 14,000 2.1
22 COD GOM 20000 INQUIRE
23 COD GBE ANY 1.5
24 HADDOCK GOM 10000 INQUIRE
25 HADDOCK GOM UP TO 6,000 0.75
26 WHITE HAKE UP TO 100,000 0.3
27 0 0 0
<class 'pandas.core.frame.DataFrame'>
我的代码:
import os
import numpy as np
import pandas as pd
import cx_Oracle
import re
from dateutil import parser
from datetime import datetime
dsnStr = cx_Oracle.makedsn("sole.noaa.gov", "1526", "sole")
con = cx_Oracle.connect(user="user", password="password$", dsn=dsnStr)
path = 'Z:\\excel_file_to_parse'
pattern = re.compile(r'Listing(.+)', re.IGNORECASE)
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)
a = pattern.findall(str(df))
print("a:", a)
new_a = str(a)
print("new_a:", new_a)
#datetime_object = datetime.strptime(new_a, '%b %d %Y')
#print("date:", datetime_object)
print("row 1:", df.loc[0])
df = df.fillna(0)
print("df wihtout 0's is:", df)
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_name, ask)" "VALUES(:3, :1, :2, :4, :5, 'Sustainable Harvest Sector', '1')")
cursor.executemany(sql_query, exported_data)
con.commit() #commit to database
cursor.close()
con.close()
exported_data
的打印输出:
exported_data: [('AVAILABLE QUOTA', 0, 0, 0, 0), ('DATE', 'TRADE ID', 'AVAILABLE STOCK', 'AMOUNT', 'SALE PRICE'), ('6/4/13', 130196, 'COD GBW', 10000, 0.6), ('5/28/13', 130158, 'COD GBW', 300, 0.6), ('6/4/13', 130210, 'HADDOCK GBE', 'UP TO 30,000', 'OFFERS'), ('5/14/13', 130079, 'PLAICE', 1000, 0.45), ('6/4/13', 130187, 'WHITE HAKE', 'UP TO 25,000', 0.5), ('6/4/13', 130208, 'WHITE HAKE', 4000, 0.5), ('6/11/13', 130249, 'WINTER GB', 3300, 0.25), ('6/4/13', 130204, 'WINTER GB', 10000, 0.48), ('5/28/13', 130148, 'WINTER GB', '1U0P 0T,0O00', 0.25), ('5/28/13', 130149, 'WINTER GB', 'UP TO 10,000', 0.4), ('5/28/13', 130157, 'WINTER GB', 1400, 0.25), ('5/21/13', 130105, 'WINTER GB', 10000, 0.5), ('5/21/13', 130106, 'WINTER GB', 10000, 0.5), ('6/4/13', 130205, 'WINTER GOM', 1000, 0.38), ('6/11/13', 130250, 'WINTER SNE', 6500, 0.4), ('6/4/13', 130206, 'WINTER SNE', 3000, 0.63), ('6/11/13', 130248, 'YELLOWTAIL GOM', 2000, 1.25), ('QUOTA TO BUY', 0, 0, 0, 0), ('DATE', 'TRADE ID', 'DESIRED STOCK', 'AMOUNT', 'BUY PRICE'), ('6/11/13', 130239, 'COD GOM', 'UP TO 14,000', 2.1), ('5/14/13', 130074, 'COD GOM', 20000, 'INQUIRE'), ('3/26/13', 130006, 'COD GBE', 'ANY', 1.5), ('5/14/13', 130075, 'HADDOCK GOM', 10000, 'INQUIRE'), ('5/7/13', 130023, 'HADDOCK GOM', 'UP TO 6,000', 0.75), ('5/14/13', 130039, 'WHITE HAKE', 'UP TO 100,000', 0.3), (1, 0, 0, 0, 0)]