Python - TypeError:期望字符串或字节对象

时间:2017-03-06 20:44:57

标签: python sql pandas dataframe cx-oracle

经过大量研究后,我无法弄清楚为什么我在代码中收到此错误。

我正在尝试将Pandas Dataframe导出到我的Oracle表。我已成功地在其他数据表上完成了数百次,但这一次不断产生错误。

这是我的Dataframe,我在其中使用pd.read_excel读取并使用简单的df['column_name'] = variable命令附加了我自己的三列:

S USTAINABLE H ARVEST S ECTOR| QUOTA LISTING APRIL 16 2013 Unnamed: 1  \
1                                                DATE           TRADE ID   
2                                            04/02/13             130014   
3                                                   0                  0   
4                                                   0                  0   
5                                                   0                  0   
6                                 FY13 QUOTA – TO BUY                  0   
7                                                DATE           TRADE ID   
8                                             3/26/13             130006   
9                                              4/9/13             130012   
10                                            3/26/13             130007   
11                                            3/26/13             130001   
12                                            3/26/13             130009   
13                                             4/9/13             130013   
14                                            3/26/13             130010   
15                                            3/26/13             130008   
16                                            3/26/13             130011   
17                                                  1                  0   

         Unnamed: 2     Unnamed: 3                     Unnamed: 4 email_year  \
1   AVAILABLE STOCK         AMOUNT                      BUY PRICE       2013   
2        WINTER SNE          12000            TRADE IN RETURN FOR       2013   
3                 0              0                   HADDOCK GOM,       2013   
4                 0              0             YELLOWTAIL GOM, OR       2013   
5                 0              0                 WITCH - OFFERS       2013   
6                 0              0                              0       2013   
7     DESIRED STOCK         AMOUNT                      BUY PRICE       2013   
8           COD GBE            ANY                         OFFERS       2013   
9           COD GBW  UP TO 100,000                            0.3       2013   
10          COD GBW            ANY                         OFFERS       2013   
11          COD GOM        INQUIRE                            1.5       2013   
12        WINTER GB            ANY                         OFFERS       2013   
13       WINTER SNE  UP TO 100,000                            0.3       2013   
14       WINTER SNE            ANY                         OFFERS       2013   
15    YELLOWTAIL GB            ANY                         OFFERS       2013   
16   YELLOWTAIL GOM            ANY  TRADE FOR GB STOCKS -\nOFFERS       2013   
17                0              0                              0       2013   

   email_month email_day  
1            4        16  
2            4        16  
3            4        16  
4            4        16  
5            4        16  
6            4        16  
7            4        16  
8            4        16  
9            4        16  
10           4        16  
11           4        16  
12           4        16  
13           4        16  
14           4        16  
15           4        16  
16           4        16  
17           4        16  

我的代码在导出行cursor.executemany(sql_query, exported_data)上失败,错误为:

Traceback (most recent call last):
  File "Z:\Code\successful_excel_pdf_code.py", line 74, in <module>
    cursor.executemany(sql_query, exported_data)
TypeError: expecting string or bytes object

以下是我的相关代码:

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')]

1)我认为错误可能来自于遍布Dataframe的很多NaN,因此我将其替换为0,但仍然失败。

2)我认为错误可能是因为尝试导出没有重要信息的前几行,所以我删除了df = df.ix[1:]的第一行,但它仍然失败。

3)我还认为由于我的email_year/month/day列中的值可能会失败,所以在将它们放入我的Dataframe之前我将它们全部更改为字符串,但它仍然失败

4)我尝试将exported_data命令更改为str而不是tuple,但只将错误更改为cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number。此外,在导出其他Dataframe时,它始终可以正常tuple

5)我认为错误可能来自我的Oracle列,不允许使用数字或字母,但它们都设置为全部VarChar2,因此这不是原因错误。

我很感激任何帮助解决这个问题,谢谢。

1 个答案:

答案 0 :(得分:2)

根据上面提到的导出数据,您遇到的问题是由于一行中的数据与后续行中的数据类型不同。在您的情况下,在一行中,您具有值'04 / 02/13'(作为字符串),在下一行中,您具有值0(作为整数)。您需要确保所有行的列的数据类型一致。