码迷,mamicode.com
首页 > 其他好文 > 详细

process data

时间:2018-02-27 19:23:13      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:base   cut   turn   fetch   one   log   data   tab   sele   

def connect_mysql(sql, oper_type="select", data_l=None):
    conn = pymysql.connect(host=localhost, user="root", password="123",
                           database="work", port=3306)
    cur = conn.cursor()
    if oper_type == "insert":
        cur.executemany(sql, data_l)
        conn.commit()
    else:
        cur.execute(sql)
    result = cur.fetchall()
    # print(type(result), "result")
    conn.close()
    return result


def process_jobs(field_name):
    sql = "select j." + field_name + " FROM personal_jobs j"
    column_name = connect_mysql(sql, oper_type="select")
    row_total = (len(column_name))
    row_category = set(column_name)

    # init category dict
    category_dict = {}
    for k in row_category:
        category_dict[k] = 0

    # calculate amount
    cal_nmu = 0
    for k in row_category:
        for r in column_name:
            if r == k:
                cal_nmu += 1
        category_dict[k] = cal_nmu
        cal_nmu = 0
    print(type(category_dict.items()), category_dict.items())
    print(row_total, len(category_dict.items()))
    return row_total, category_dict

process_jobs("job_salary")

 

process data

标签:base   cut   turn   fetch   one   log   data   tab   sele   

原文地址:https://www.cnblogs.com/vickey-wu/p/8480227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!