常用自定义函数总结

获取文件路径

def get_file_path(rootdir):
    for (dirpath, dirnames, filenames) in os.walk(rootdir):
        pathname = [os.path.join(dirpath, filename) for filename in filenames]
    return pathname

日期转时间戳

def time2stamp(timestr):
    timeArray = time.strptime(timestr, "%Y-%m-%d %H:%M")
    # year = timeArray.tm_year
    timeStamp = int(time.mktime(timeArray))
    return timeStamp

时间戳转日期(pandas处理数据时,值为空时类型是NaN)

def stamp2time(timestamp, dt=''):  # 时间戳转日期函数
    if not timestamp is np.NaN:
        dt = time.strftime("%Y-%m-%d %H:%M", time.localtime(int(timestamp)))
    return dt

pandas写入数据库

def write2sql(df, tableName):
    con = create_engine('mysql+pymysql://user:password@host:3306/database?charset=utf8')
    df.index = range(1, len(df) + 1)
    df.to_sql(tableName, con, schema='database', if_exists='append', index=True, index_label='id')  # 可加参数chunksize=10000; 备注:index为false时则不按照索引往数据库写数据

你可能感兴趣的:(python)