pandas的dtype处理
dtype的格式化处理,参考
补充代码
from sqlalchemy.types import NVARCHAR, Float, Integer,Text
from sqlalchemy.dialects.mysql import LONGTEXT
def mapping_df_types(df):
dtypedict = {}
for i, j in zip(df.columns, df.dtypes):
if "object" in str(j):
dtypedict.update({i: NVARCHAR(length=60)})
if "favIconUrl" == i:
dtypedict.update({i: LONGTEXT()})
print(i)
if "url" == i:
dtypedict.update({i: Text()})
print(i)
if "title" == i:
dtypedict.update({i: Text()})
print(i)
if "float" in str(j):
dtypedict.update({i: Float(precision=2, asdecimal=True)})
if "int" in str(j):
dtypedict.update({i: Integer()})
return dtypedict
dtypedict = mapping_df_types(df)
print(dtypedict)
table_name="ods_chrome_view_tab"
df.to_sql(df,table_name,if_exists_type='replace', dtype=dtypedict)

浙公网安备 33010602011771号