我正在使用 pandas.read_sql 檢索表名串列,然后嘗試使用“for”回圈從檢索到的串列中洗掉表。但是,我收到一個 'NoneType' object is not iterable 錯誤(盡管串列不為空)。
print(type(out_tables_list))
<class 'list'>
print(out_tables_list is None)
False
tables = pd.read_sql("""
SHOW TABLES IN P_SMO_INP_T
""", con=connection)
tableName = tables.tableName
out_tables = tableName[tableName.str.contains('_sg_')]
out_tables_list = out_tables.to_list()
for name in out_tables_list:
pd.read_sql("DROP TABLE P_SMO_INP_T.{}".format(name), con=connection)
你能幫我弄清楚我的錯誤是什么嗎?先感謝您!
LE:請參閱下面我收到的完整輸出:
TypeError Traceback (most recent call last)
<ipython-input-46-1e7a629dac7d> in <module>
1 for name in out_tables_list:
----> 2 pd.read_sql("DROP TABLE P_SMO_INP_T.{}".format(name), con=connection)
c:\users\sgulunga\appdata\local\programs\python\python38-32\lib\site-packages\pandas\io\sql.py in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize)
481
482 if isinstance(pandas_sql, SQLiteDatabase):
--> 483 return pandas_sql.read_query(
484 sql,
485 index_col=index_col,
c:\users\sgulunga\appdata\local\programs\python\python38-32\lib\site-packages\pandas\io\sql.py in read_query(self, sql, index_col, coerce_float, params, parse_dates, chunksize)
1726 args = _convert_params(sql, params)
1727 cursor = self.execute(*args)
-> 1728 columns = [col_desc[0] for col_desc in cursor.description]
1729
1730 if chunksize is not None:
TypeError: 'NoneType' object is not iterable
uj5u.com熱心網友回復:
一個DROP查詢回傳什么都沒有,所以None,所以大熊貓不能迭代上,要建立一個資料幀。
請參閱當我沒有表物件時如何在 SQLAlchemy 中洗掉表?
請注意,除錯時要小心引發錯誤的行,您說“盡管串列不為空”但引發錯誤的行不使用串列
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/399126.html
