python字典嵌套实例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

#coding:UTF-8
import os 
import sys 
import collections


#文件内容如下(获得)
#[2018春节爆字统计]用户(123456)获得福字(0)(1)​

​#文件内容如下(消耗)
​#[2018春节会员兑换统计]用户(888888)进行会员兑换(0), 目前等级(2), 到期时间(1519997460), 兑换前等级(1), 到期时间(1519997456)​

file_get = "./TheSpringFestival_get"
file_consum ="./TheSpringFestival_xiao"

#定义空字典存储数据
dict_get = {}

#解析每一行数据(获得)
def get_data(tmp_line):
splist_result = tmp_line.split('(');
usernum = splist_result[1].split(')')[0]
key_nums = splist_result[2].split(')')[0]
key_count = splist_result[3].split(')')[0]
return usernum,key_nums

#解析每一行数据(消耗)
def get_data2(tmp_line):
splist_result = tmp_line.split('(');
usernum = splist_result[1].split(')')[0]
return usernum

#分析判断并保存数据 
def func_ana_get():
print "Enter func !"
fp_get = open(file_get,"a+");
get_lins = fp_get.readlines();
for tmp_line in get_lins:
#print tmp_line
user,key = get_data(tmp_line);
if dict_get.has_key(user):
if dict_get[user].has_key(key):
dict_get[user][key] = dict_get[user][key]+1 
else:
dict_get[user][key] = 1
pass
else:
tmp_dict = {};
tmp_dict[key] = 1;
dict_get[user] = tmp_dict
fp_get.close()

#消耗 
def func_ana_xiao():
print "Enter func !"
fp_get = open(file_consum,"a+");
get_lins = fp_get.readlines();
for tmp_line in get_lins:
#print tmp_line.strip()
user = get_data2(tmp_line)
if dict_get.has_key(user):
for keys in dict_get[user]:
dict_get[user][keys] = dict_get[user][keys]-1
else:
pass
fp_get.close()

​#统计字典中各值的和
def get_sum(user_data):
sum = 0
for keys in user_data:
sum = sum + user_data[keys]
return sum

#统计排序
def func_ana_tonji():
sort_dict ={}
for keys in dict_get:
sum = get_sum(dict_get[keys])
sort_dict[keys] = sum

aa = sorted(sort_dict.items(),key=lambda  item:item[1],reverse=True)
print aa
nn = 0
for  keys  in aa:
    print keys[0],keys[1]  
    if  nn > 10:
        break
    nn = nn+1

func_ana_get();

func_ana_xiao();

func_ana_tonji(); 






​#!/usr/bin/env python
#--coding:utf-8--
#会员鲜花库存统计
import MySQLdb
import os, sys, re, string
import time, getopt

optmap = {
'dbuser' : 'haoren',
'dbpass' : 'ddddd',
'dbhost' : '172.17.1.14',
'dbhost_gm' : '172.17.1.13',
'dbport' : 3306,
'dbname' : 'PIWMDB',
'dbname_gm' : 'TGTMDB'
}

def main():
one_day = time.strftime("%Y%m%d", time.localtime(time.time() - 246060))
opts, args = getopt.getopt(sys.argv[1:], 'd:')
for op, value in opts:
if op == '-d':
m = re.search('[0-9]{8}', value)
if m:
one_day = value
else:
print "请输入8位日期(比如:20130215)"
return 'no'
print "正在统计会员鲜花库存(%s)..." %one_day

    db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], db=optmap['dbname'])
    db_conn.query("use %s" %optmap['dbname'])
    db_cursor = db_conn.cursor()

    vip_user_list = {}
    for i in range(10):
            sql = "select USERID, VIPSTATE from VIPUSER%s" %i
            print sql
            db_cursor.execute(sql)
            db_rows = db_cursor.fetchall()
            for USERID, VIPSTATE in db_rows:
                    vip_user_list[USERID] = VIPSTATE

    vip_user_flower_list = {}
    for i in range(10):
            sql = "select USERID, FLOWER from VIPUSERFLOWER%s" %i
            print sql
            db_cursor.execute(sql)
            db_rows = db_cursor.fetchall()
            for USERID, FLOWER in db_rows:
                    vip_user_flower_list[USERID] = FLOWER

    vip_state_flower_list = {}
    vip_state_flower_list[1] = 0;
    vip_state_flower_list[2] = 0;
    vip_state_flower_list[3] = 0;
    for key in vip_user_list:
            if key in vip_user_flower_list:
                    if vip_user_list[key] in vip_state_flower_list:
                            vip_state_flower_list[vip_user_list[key]] += vip_user_flower_list[key]

    for key in vip_state_flower_list:
            print key, vip_state_flower_list[key]

    db_cursor.close()
    db_conn.close()

    db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost_gm'], port=optmap['dbport'], db=optmap['dbname_gm'])
    db_conn.query("use %s" %optmap['dbname_gm'])
    db_cursor = db_conn.cursor()

    dword_time = time.mktime(time.strptime(one_day, '%Y%m%d'))

    sql = "update VIPUSERFLOWERMONTHLY set year_flower_left_num=%d, month_flower_left_num=%d, week_flower_left_num=%d where count_time='%d'" %(vip_state_flower_list[3], vip_state_flower_list[2], vip_state_flower_list[1], dword_time)
    print sql
    db_conn.query(sql)

    db_conn.commit()

    db_cursor.close()
    db_conn.close()

main()
#if name == "main":

main()



​​


​​
​#!/usr/bin/env python
#--coding:utf-8--
#会员信息统计
import MySQLdb
import os, sys, re, string
import time, getopt

optmap = {
'dbuser' : 'haoren',
'dbpass' : 'ddddd',
'dbhost' : '172.17.1.13',
'dbport' : 3306,
'dbname' : 'GTMDB',
'logdir' : '/home/haoren/logdir/', #外网环境日志目录
'logpattern' : '^sessionserver.log.' #外网环境日志名称前缀
}

def get_files(dir, pattern):
print dir, pattern
match_file_list = []
if os.path.exists(dir):
cur_file_list = os.listdir(dir)
for file_name in cur_file_list:
if re.search(pattern, file_name):
match_file_list.append(file_name)
return match_file_list
else:
return 'no'

def main():
one_day = time.strftime("%Y%m%d", time.localtime(time.time() - 246060)) #默认日期为脚本运行的上一天
opts, args = getopt.getopt(sys.argv[1:], 'd:')
for op, value in opts:
if op == '-d':
m = re.search('[0-9]{8}', value)
if m:
one_day = value
else:
print "请输入8位日期(比如:20130215)"
return 'no'

    print "正在读取VIP用户数据(%s)..." %one_day
    db_conn = MySQLdb.connect(user=optmap['dbuser'], passwd=optmap['dbpass'], host=optmap['dbhost'], port=optmap['dbport'], db=optmap['dbname'])
    db_cursor = db_conn.cursor()

    temp_vip_active_user_num_file_name = '/tmp/vipactiveusernumtemp.txt'
    command = "cat /dev/null > %s" %(temp_vip_active_user_num_file_name)
    os.system(command)

    if re.search('haoren', optmap['logdir']):
            print '外网环境'
            log_dir_name_list = get_files(optmap['logdir'], one_day[2:])
            for log_dir_name_item in log_dir_name_list:
                    log_dir_full_path = optmap['logdir']+log_dir_name_item+'/'
                    log_file_name_list = get_files(log_dir_full_path, optmap['logpattern'] + one_day[2:])
                    for log_file_name_item in log_file_name_list:
                            print log_file_name_item
                            command = "cat %s%s |awk '/用户登录/' |awk '/vip状态/' >> %s" % (log_dir_full_path, log_file_name_item, temp_vip_active_user_num_file_name)
                            os.system(command)
    else:
            print '内网环境'
            log_file_name_list = get_files(optmap['logdir'], optmap['logpattern'] + one_day[2:])
            for log_file_name_item in log_file_name_list:
                    command = "cat %s%s |awk '/用户登录/' |awk '/vip状态/' >> %s" % (optmap['logdir'], log_file_name_item, temp_vip_active_user_num_file_name)
                    os.system(command)

    command = "cat %s |wc -l" %temp_vip_active_user_num_file_name
    os.system(command)

    #一天当中用户可能从月会员降级到周会员,造成不同会员状态的同一帐号统计两次,所以总会员!=年会员+月会员+周会员)
    #不同状态的会员用同一计算机登录,所以总mac/ip!=年mac/ip+月mac/ip+周mac/ip
    total_account_map = {}
    total_mac_map = {}
    total_ip_map = {}
    before_account_map = {}
    before_mac_map = {}
    before_ip_map = {}

    account_map = {1:{}, 2:{}, 3:{}, 11:{}, 12:{}, 13:{}}
    mac_map = {1:{}, 2:{}, 3:{}, 11:{}, 12:{}, 13:{}}
    ip_map = {1:{}, 2:{}, 3:{}, 11:{}, 12:{}, 13:{}}

    temp_vip_active_user_num_file = open(temp_vip_active_user_num_file_name)
    for one_line in temp_vip_active_user_num_file.readlines():
            match = re.search("^(\S+) SS\[\d+\] TRACE: 用户登录:imid:(\d+),mac地址:(\d+),ip地址:(\d+),vip状态:(\d+),登录时间:(\d+)(\S+)", one_line)
            if match:
                    if string.atoi(match.group(5)) in (1, 2, 3):
                            total_account_map[string.atoi(match.group(2))] = string.atoi(match.group(5))
                            total_mac_map[string.atoi(match.group(3))] = string.atoi(match.group(5))
                            total_ip_map[string.atoi(match.group(4))] = string.atoi(match.group(5))
                    elif string.atoi(match.group(5)) in (11, 12, 13):
                            before_account_map[string.atoi(match.group(2))] = string.atoi(match.group(5))
                            before_mac_map[string.atoi(match.group(3))] = string.atoi(match.group(5))
                            before_ip_map[string.atoi(match.group(4))] = string.atoi(match.group(5))
                    account_map[string.atoi(match.group(5))][string.atoi(match.group(2))] = string.atoi(match.group(3))
                    mac_map[string.atoi(match.group(5))][string.atoi(match.group(3))] = string.atoi(match.group(2))
                    ip_map[string.atoi(match.group(5))][string.atoi(match.group(4))] = string.atoi(match.group(2))
    temp_vip_active_user_num_file.close()

    dword_time = time.mktime(time.strptime(one_day, '%Y%m%d'))
    db_conn.query("use %s" %optmap['dbname'])
    sql = "delete from VIPACTIVEUSERNUM where active_time='%d'" %dword_time
    print sql
    db_conn.query(sql)

    sql = "insert into VIPACTIVEUSERNUM (active_time) values('%d')" %(dword_time)
    print sql
    db_conn.query(sql)

    sql = "update VIPACTIVEUSERNUM set year_account_num=%d, year_mac_num=%d, year_ip_num=%d, month_account_num=%d, month_mac_num=%d, month_ip_num=%d, week_account_num=%d, week_mac_num=%d, week_ip_num=%d, total_mac_num=%d, total_ip_num=%d, before_account_num=%d, before_mac_num=%d, before_ip_num=%d where active_time='%d'" %(len(account_map[3]), len(mac_map[3]), len(ip_map[3]), len(account_map[2]), len(mac_map[2]), len(ip_map[2]), len(account_map[1]), len(mac_map[1]), len(ip_map[1]), len(total_mac_map), len(total_ip_map), len(before_account_map), len(before_mac_map), len(before_ip_map), dword_time)
    print sql
    db_conn.query(sql)

    db_conn.commit()

    db_cursor.close()
    db_conn.close()

main()
#if name == "main"

main()










本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/2074444,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
16天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
20小时前
|
存储 机器学习/深度学习 数据可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
12 6
|
20小时前
|
机器学习/深度学习 数据可视化 算法
PYTHON用决策树分类预测糖尿病和可视化实例
PYTHON用决策树分类预测糖尿病和可视化实例
|
1天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
|
2天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
3天前
|
Python
python学习12-类对象和实例对象
python学习12-类对象和实例对象
|
3天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
7天前
|
安全 Python
python字典的内置方法
Python字典主要方法包括:`keys()`(返回所有键)、`values()`(返回所有值)、`items()`(返回所有键值对)、`get()`(安全取值,键不存在时返回默认值)、`setdefault()`(设置默认值)、`update()`(合并字典)、`pop()`(删除并返回值)、`clear()`(清空字典)、`copy()`(浅拷贝)、`fromkeys()`(新建字典并设置默认值)、`popitem()`(随机删除键值对)。
7 0
|
16天前
|
存储 Java 程序员
【Python】6. 基础语法(4) -- 列表+元组+字典篇
【Python】6. 基础语法(4) -- 列表+元组+字典篇
39 1
|
16天前
|
存储 Python
python基础篇: 详解 Python 字典类型内置方法
python基础篇: 详解 Python 字典类型内置方法
25 1

热门文章

最新文章