【问题标题】:Can I bypass a TypeError in order to execute my command?我可以绕过 TypeError 来执行我的命令吗?
【发布时间】:2017-03-07 15:52:14
【问题描述】:

我正在尝试将 Dataframe 导出到 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 为 cursor.executemany(sql_query, exported_data, batcherrors=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')]

我真的很想帮助解决这个问题,因为我已经坚持了一个多星期了。谢谢。

【问题讨论】:

  • 请将相关代码sn-ps添加到这个问题中,而不是参考您的其他问题。

标签: python sql oracle pandas typeerror


【解决方案1】:

Error or exception handling in Python is done by using try-except blocks

try:
    cursor.executemany(sql_query, exported_data)
except TypeError:
    pass # put your error handling code here, pass will ignore the error

您要如何处理错误取决于您自己。默认的 Python 行为是 raise 错误(因此您会看到它)。当遇到错误时,执行将停止,并执行您定义的错误处理例程。忽略错误不会使cursor.executemany 方法从中断处继续,而是根本不处理错误。您可以在那里再次调用该方法(使用相同的参数),但这显然无济于事,因为它只会再次产生相同的错误。

【讨论】:

  • 您确实完美地回答了我的问题,但导出仍然失败。相信我,我已经把它分解成我能做到的最小和最具体的部分,但我无法弄清楚。我尝试将exported_data 设为str 而不是tuple,但它仍然失败,我已将NaNs 替换为0,我已将Dataframe 中的所有值更改为字符串,我已确认导出到表格适用于简单的单字导出等。但我想即使绕过阻碍我的错误仍然不允许导出
  • 我会在答案中添加更多关于您的期望的信息。
  • 但是没有解决根本问题的想法?
  • 您的 data_posted 和 species 值有时是数字,有时是字符串。我猜你不应该用0来填空,而是用空字符串''。
  • 老实说,我认为这可以解决这个问题,这是一个非常聪明的想法,并且考虑到错误 expecting string or bytes........ 但这也没有解决它:(跨度>
猜你喜欢
  • 1970-01-01
  • 2018-04-12
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 2016-11-15
  • 2018-08-02
  • 2017-03-22
  • 1970-01-01
相关资源
最近更新 更多