预期的字符串或字节对象

时间:2018-08-21 14:02:06

标签: python python-3.x oracle pandas cx-oracle

我正在制作一个excel电子表格,将其加载到Pandas Dataframe中,然后将其添加到Oracle表中。

任何可能敏感的数据都已加星标。

timeto

但是,我收到错误import os import pandas as pd import cx_Oracle from tkinter import Tk from tkinter.filedialog import askopenfilename from sqlalchemy import create_engine #Gets excel file and writes to dataframe Tk().withdraw() filename = askopenfilename() xl = pd.ExcelFile(filename) print(xl.sheet_names) ws = input('Type a sheet name to load: \n') dFrame = xl.parse(ws) #create engine connection engine = create_engine('oracle+cx_oracle://*********') print("Engine created") with engine.begin() as conn: conn.execute("DELETE FROM table") print("Delete from executed") #Write dataframe to table dFrame.to_sql('table',con=engine, if_exists='append', index=False) print("Data frame appended to table") (下面有完整的跟踪信息),我认为这意味着数据框中存在与现有表类型不匹配的列。

如果是这种情况,如何找到数据框的哪个部分引起了问题?

TypeError: expecting string or bytes object

编辑:

这分别是pandas和oracle中每一列的数据类型:

Traceback (most recent call last):
  File "C:\Users\*****\Documents\CD\Test.py", line 26, in <module>
    dFrame.to_sql('table',con=engine, if_exists='append', index=False)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 2130, in to_sql
    dtype=dtype)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 450, in to_sql
    chunksize=chunksize, dtype=dtype)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 1127, in to_sql
    table.insert(chunksize)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 641, in insert
    self._execute_insert(conn, keys, chunk_iter)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\sql.py", line 616, in _execute_insert
    conn.execute(self.insert_statement(), data)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\sql\elements.py", line 269, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1060, in _execute_clauseelement
    compiled_sql, distilled_params
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1200, in _execute_context
    context)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1416, in _handle_dbapi_exception
    util.reraise(*exc_info)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\util\compat.py", line 249, in reraise
    raise value
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\base.py", line 1170, in _execute_context
    context)
  File "C:\Users\******\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\dialects\oracle\cx_oracle.py", line 969, in do_executemany
    cursor.executemany(statement, parameters)
TypeError: expecting string or bytes object

这些都不应该引起问题,对吧?

2 个答案:

答案 0 :(得分:1)

您可以尝试打印数据帧“ dFrame”的数据类型并检查oracle表的相应数据类型

  

print(dFrame.dtypes)

希望它可以为您提供一些线索。

答案 1 :(得分:0)

请检查您的数据框中是否有“ Nan”吗? 实际上,昨天我遇到了同样的错误,并且在脚本中添加了以下代码之后,数据帧已成功附加到oracle表中。

df = df.fillna('na')