[英]Expecting string or bytes object
I am taking an excel spreadsheet, loading into a Pandas Dataframe then appending the data frame to an Oracle table. 我正在准备一个Excel电子表格,将其加载到Pandas Dataframe中,然后将数据框附加到Oracle表中。
Any potentially sensitive data is starred. 任何可能敏感的数据都已加星标。
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")
However, I get error TypeError: expecting string or bytes object
(full trace below), I believe this means there is a column in the dataframe which does not match the existing table type. 但是,我收到错误
TypeError: expecting string or bytes object
(下面为完整跟踪),我认为这意味着数据框中存在与现有表类型不匹配的列。
If that is the case, how can I find which section of the dataframe is causing a problem? 如果是这样,我如何找到数据框的哪个部分引起了问题?
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
EDIT: 编辑:
This is the datatypes for each column in pandas and oracle, respectively: 这分别是pandas和oracle中每一列的数据类型:
Column1 | int64 | NUMBER(10,0)
Column2 | object | VARCHAR2(128 BYTE)
Column3 | int64 | NUMBER(13,0)
Column4 | object | VARCHAR2(128 BYTE)
Column5 | object | VARCHAR2(128 BYTE)
Column6 | float64 | NUMBER(10,0)
Column7 | object | VARCHAR2(128 BYTE)
Column8 | object | VARCHAR2(128 BYTE)
Column9 | int64 | NUMBER(5,0)
Column10| datetime64[ns] | DATE
Column11| datetime64[ns] | DATE
Column12| int64 | NUMBER(10,0)
Column13| float64 | NUMBER(10,0)
Column14| object | VARCHAR2(128 BYTE)
Column15| float64 | NUMBER(10,0)
None of those should be causing issues, right? 这些都不应该引起问题,对吗?
would you please check if any 'Nan' in your Dataframe? 您能检查一下数据框中是否有“ Nan”吗? Actually I got the same error yesterday, and after add below code in my script, the dataframe has been appended to a oracle table successfully.
实际上,昨天我遇到了同样的错误,并且在脚本中添加了以下代码之后,数据帧已成功附加到oracle表中。
df = df.fillna('na')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.