import pandas as pd
import pymysql
import datetime

def con_sql(db,sql):
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        result = cursor.fetchall()
        df = pd.DataFrame(list(result))
    except Exception:
        print("发生异常", Exception)
        df = pd.DataFrame()
    finally:
        db.close()
    return df


def multi():
    db = pymysql.connect(host='10.66.157.22', port=4000, user='root', passwd='3SYz54LS9#^9sBvC', db='jerry_test')
    sql = "select diary_id,level2_ids from diary_feat"
    df = con_sql(db, sql).dropna()
    print(df.shape)
    df = df.rename(columns={0: "cid", 1: "level"})
    df["l1"] = "lost"
    df["l2"] = "lost"
    df["l3"] = "lost"
    for i in list(df["level"].unique()):
        l = i.split(",")
        if len(l) == 3:
            df.loc[df["level"] == i, ["l1"]] = l[0]
            df.loc[df["level"] == i, ["l2"]] = l[1]
            df.loc[df["level"] == i, ["l3"]] = l[2]
        elif len(l) == 2:
            df.loc[df["level"] == i, ["l1"]] = l[0]
            df.loc[df["level"] == i, ["l2"]] = l[1]
        elif len(l) == 1:
            df.loc[df["level"] == i, ["l1"]] = l[0]
    df = df.drop("level",axis=1)
    print(df.head())
    a = list(df["l1"].unique())
    b = list(df["l2"].unique())
    c = list(df["l3"].unique())
    print(len(a))
    print(a)
    print(len(b))
    print(b)
    print(len(c))
    print(c)


if __name__ == "__main__":
    multi()
