Python: Convert email address to HTML link
Question: I'm looking for a stand-alone python function that will take in a string and
return a string with the email addresses converted to links.

Example:

    
    
    >>>s = 'blah blah blah a@at.com blah blah blah'
    >>>link(s)
    'blah blah blah <a href="mailto:a@at.com">a@at.com</a> blah blah blah'
    


Answer: Something like this?

    
    
    import re
    import xml.sax.saxutils
    
    def anchor_from_email_address_match(match):
        address = match.group(0)
        return "<a href=%s>%s</a>" % (
            xml.sax.saxutils.quoteattr("mailto:" + address),
            xml.sax.saxutils.escape(address))
    
    def replace_email_addresses_with_anchors(text):
        return re.sub("\w+@(?:\w|\.)+", anchor_from_email_address_match, text)
    
    print replace_email_addresses_with_anchors(
        "An address: bob@example.com, and another: joe@example.com")
    

