我试图将一个数据帧导出到Oracle表中,并一直遇到错误
TypeError: expecting string or bytes object
。在
我想能够忽略或绕过这个错误,以便导出我所拥有的。有可能吗?在
这里有一个链接,我将详细解释我的问题:Python - TypeError: expecting string or bytes object。老实说,我的数据看起来很完美,列数和行数都是正确的,它们都是相同的数据类型,我以前用过这个方法导出了数百个其他的数据帧,我想绕过错误消息,以便导出到目前为止的数据。在
另外,因为它在cursor.executemany(
行上失败,所以我决定研究这个命令。以下是cx_Oracle
:http://cx-oracle.readthedocs.io/en/latest/cursor.html的文档。它声明:
When true, the batcherrors parameter enables batch error support within Oracle and ensures that the call succeeds even if an exception takes place in one or more of the sequence of parameters.
所以我将它设置为true,它没有改变任何东西。在
以下是我的相关代码:
df = pd.read_excel(file_path)
df = df.fillna(0)
df = df.ix[1:]
cursor = con.cursor()
exported_data = [tuple(x) for x in df.values]
#exported_data = [str(x) for x in df.values]
#print("exported_data:", exported_data)
sql_query = ("INSERT INTO FISHTABLE(date_posted, stock_id, species, pounds, advertised_price, email_year, email_month, email_day, sector_name, ask)" "VALUES(:1, :2, :3, :4, :5, :6, :7, :8, 'Sustainable Harvest Sector', '1')")
cursor.executemany(sql_query, exported_data)
con.commit() #commit to database
cursor.close()
con.close()
下面是exported_data
的打印输出:
[('DATE', 'TRADE ID', 'AVAILABLE STOCK', 'AMOUNT', 'BUY PRICE', '2013', '4', '16'), ('04/02/13', 130014, 'WINTER SNE', 12000, 'TRADE IN RETURN FOR', '2013', '4', '16'), (0, 0, 0, 0, 'HADDOCK GOM,', '2013', '4', '16'), (0, 0, 0, 0, 'YELLOWTAIL GOM, OR', '2013', '4', '16'), (0, 0, 0, 0, 'WITCH - OFFERS', '2013', '4', '16'), ('FY13 QUOTA – TO BUY', 0, 0, 0, 0, '2013', '4', '16'), ('DATE', 'TRADE ID', 'DESIRED STOCK', 'AMOUNT', 'BUY PRICE', '2013', '4', '16'), ('3/26/13', 130006, 'COD GBE', 'ANY', 'OFFERS', '2013', '4', '16'), ('4/9/13', 130012, 'COD GBW', 'UP TO 100,000', 0.3, '2013', '4', '16'), ('3/26/13', 130007, 'COD GBW', 'ANY', 'OFFERS', '2013', '4', '16'), ('3/26/13', 130001, 'COD GOM', 'INQUIRE', 1.5, '2013', '4', '16'), ('3/26/13', 130009, 'WINTER GB', 'ANY', 'OFFERS', '2013', '4', '16'), ('4/9/13', 130013, 'WINTER SNE', 'UP TO 100,000', 0.3, '2013', '4', '16'), ('3/26/13', 130010, 'WINTER SNE', 'ANY', 'OFFERS', '2013', '4', '16'), ('3/26/13', 130008, 'YELLOWTAIL GB', 'ANY', 'OFFERS', '2013', '4', '16'), ('3/26/13', 130011, 'YELLOWTAIL GOM', 'ANY', 'TRADE FOR GB STOCKS -\nOFFERS', '2013', '4', '16'), (1, 0, 0, 0, 0, '2013', '4', '16')]
我真的很想帮忙解决这个问题,因为我已经坚持了一个多星期了。谢谢。在
Error or exception handling in Python is done by using try-except blocks
你想如何处理错误取决于你自己。Python的默认行为是
raise
错误(因此您可以看到它)。当遇到错误时,将在该点停止执行,并执行您定义的错误处理例程。忽略错误将不会使方法cursor.executemany
从它停止的地方恢复,而是简单地不处理错误。您可以在那里再次调用该方法(使用相同的参数),但这显然没有帮助,因为它只会再次产生相同的错误。在相关问题 更多 >
编程相关推荐