标签:python
按公司要求把已经存在的城市+部门(bjxxx,xmxxx)邮件组改成部门+城市(xxxbj,xxxxm)格式。
注意:原来的bjxxx组里已经有人员,需要把原来的人员都删除,然后再添加xxxbj邮件组。
#!/usr/bin/python
import sys
sys.path.append(‘/sw/systems/shared/python‘)
from DB import DB
con,cur = DB().connect()
#从数据库里列出符合bjxxx的邮件组
def mail_forward():
query = """ SELECT name,forward_name from mail.forward WHERE `name` like ‘bj%‘ or `name` like ‘xm%‘ or `name` like ‘wx%‘
and name != ‘wxa‘
"""
cur.execute(query)
results = cur.fetchall()
forwards = {}
for result in results:
name = result[‘name‘]
forward_name = result[‘forward_name‘].lower().strip()
forwards.setdefault(name,[])
forwards[name].append(forward_name)
return forwards
forwards = mail_forward()
#把bjxxx格式变成xxxbj格式
new_deps = {}
for f in forwards:
str1 = f[0:2]
str2 = f[2:]
str2 +=str1
new_deps.setdefault(f,str2)
try:
for dep,users in forwards.items():
print dep,users
for user in users:
delete = ‘delete from mail.forward where name = "%s" and forward_name = "%s"‘ % (dep, user)
cur.execute(delete)
if dep in new_deps.keys():
insert = ‘insert ignore into mail.forward (name,forward_name) values("%s","%s")‘ % (dep,new_deps[dep])
cur.execute(insert)
con.commit()
except Exception,e:
print type(e),e标签:python
原文地址:http://8287056.blog.51cto.com/8277056/1954571