python使用sqlalchemy從mysql獲取數據後調整爲mysql的數據格式

def get_data_from_mysql_table(db_url, is_debug=False):
    '''pandas>=0.24.0'''
    engine = create_engine(db_url, echo=is_debug)
    conn = engine.connect()

    #獲取數據表字段的類型
    meta_data = MetaData(bind=engine, reflect=True) 
    cols,types = [],[]
    columns = meta_data.tables[table].columns
    for col in columns:
        cols.append(col.name)
        types.append(str(col.type))
    df_col = pd.DataFrame({"COLUMN": cols, "DTYPE": types})

    #獲取數據
    sql = "SELECT * FROM DB.TABLE limit 5000".format(table,column)
    df = pd.read_sql(sql, conn)

    #類型轉換
    if df.shape[1] == 0: #過濾 pd.DataFrame() 
        print("Error! input a wrong param.")
        sys.exit(1)
    for indexs in df_col.index:
        col = df_col.loc[indexs].COLUMN
        dtype = df_col.loc[indexs].DTYPE
        if dtype.find('INT')>= 0:
            df[col] = pd.to_numeric(df[col], errors='coerce')
            df[col] = df[col].astype('Int32')
        elif dtype.find('VARCHAR')>= 0:
            df[col] = df[col].astype('str')
        elif dtype.find('DOUBLE')>= 0:
            df[col] = pd.to_numeric(df[col], errors='coerce')
            df[col] = df[col].astype('float')
        elif dtype.find('FLOAT')>= 0:
            df[col] = pd.to_numeric(df[col], errors='coerce')
            df[col] = df[col].astype('float')
        elif dtype.find('DATETIME')>= 0:
            utc_times = pd.to_datetime(df[col].tolist(), errors='coerce', utc=False)
            df[col]= utc_times.tz_localize('Asia/Shanghai') 
        elif dtype.find('DATE')>= 0:
            df[col] = pd.to_datetime(df[col],format='%Y-%m-%d', errors='coerce')
            utc_times = pd.to_datetime(df[col].tolist(),format='%Y-%m-%d',errors='coerce', utc=False)
            df[col]= utc_times.tz_localize('Asia/Shanghai')
        else:
            pass
    return df 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章