- 论坛徽章:
- 4
|
- [liangdong@bb-browser-test00.vm.baidu.com py_project]$ ./main.py emails.txt
- [liangdong@bb-browser-test00.vm.baidu.com py_project]$ ll
- total 16
- -rw-rw-r-- 1 liangdong liangdong 49 Aug 7 12:40 emails.txt
- -rw-rw-r-- 1 liangdong liangdong 32 Aug 7 12:52 gmail.com
- -rw-rw-r-- 1 liangdong liangdong 17 Aug 7 12:52 hotmail.com
- -rwxrwxr-x 1 liangdong liangdong 638 Aug 7 12:52 main.py
- [liangdong@bb-browser-test00.vm.baidu.com py_project]$ cat main.py
- #!/usr/local/bin/python
- # -*- coding: utf-8 -*-
- import re
- import sys
- def emails_classify(file_name):
- dest_dict = {}
- try:
- with open(file_name) as src_fp:
- for line in src_fp:
- email = line.strip()
- match_obj = re.search(r'^\w+@(\w+\.\w+), email)
- if match_obj:
- domain = match_obj.group(1)
- if domain not in dest_dict:
- dest_dict[domain] = open(domain, "w") # 'w' for test
- dest_fp = dest_dict[domain]
- dest_fp.write(email + "\n")
- except:
- return -1
- finally:
- for dest_domain, dest_fp in dest_dict.iteritems():
- dest_fp.close()
- return 0
- if len(sys.argv) > 1:
- emails_classify(sys.argv[1])
复制代码 |
|