Guest User

Untitled

a guest
Jul 26th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. >>> email.utils.getaddresses(['[email protected], John Smith <[email protected]>,"Smith, Jane" <[email protected]>'])
  2. [('', '[email protected]'), ('John Smith', '[email protected]'), ('Smith, Jane', '[email protected]')]
  3.  
  4. from email.utils import getaddresses
  5.  
  6. addrstring = ',[email protected], John Smith <[email protected]>,"Smith, Jane" <[email protected]>,'
  7.  
  8. def addrparser(addrstring):
  9. addrlist = ['']
  10. quoted = False
  11.  
  12. # ignore comma at beginning or end
  13. addrstring = addrstring.strip(',')
  14.  
  15. for char in addrstring:
  16. if char == '"':
  17. # toggle quoted mode
  18. quoted = not quoted
  19. addrlist[-1] += char
  20. # a comma outside of quotes means a new address
  21. elif char == ',' and not quoted:
  22. addrlist.append('')
  23. # anything else is the next letter of the current address
  24. else:
  25. addrlist[-1] += char
  26.  
  27. return getaddresses(addrlist)
  28.  
  29. print addrparser(addrstring)
  30.  
  31. [('', '[email protected]'), ('John Smith', '[email protected]'),
  32. ('Smith, Jane', '[email protected]')]
Advertisement
Add Comment
Please, Sign In to add comment