0


I am newbie to python and have no time to study python with more time. Here I post to look for help to realize one very simple function: search content to find out all "@gmail.com" email address.

For instance, if one string is " hello, this is test from abc@gmail.com to d123@gmail.com". Maybe I need one simple code to find out "abc@gmail.com" and "d123@gmail.com".

Is that very simple ? But I never use python and really have no idea to achieve that.

Thanks,

垃圾帖?
提问于2009-12-02 02:54:10
1 1
添加评论
1


def grab_email(files = []):
# if passed a list of text files, will return a list of
# email addresses found in the files, matched according to
# basic address conventions. Note: supports most possible
# names, but not all valid ones.

found = []
if files != None:
    mailsrch = re.compile(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}')

    for file in files:            
        for line in open(file,'r'):                
            found.extend(mailsrch.findall(line))    

# remove duplicate elements
# borrowed from Tim Peters' algorithm on ASPN Cookbook
u = {}
for item in found:
    u[item] = 1

# return list of unique email addresses
return u.keys()
永久链接 | 垃圾帖?
回答于2010-01-01 12:25:10
11 2
添加评论




Made with Django.

当前版本: R-0127-20090523

cc-wiki