Posted By

backlashblues on 11/30/09


Tagged

regex email python script regular Expression


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

chandchv


python script to extract all email addresses from bulk text


 / Published in: Python
 

testing tool: http://www.pythonregex.com/

  1. # this script will open a file with email addresses in it, then extract
  2. # those address and write them to a new file
  3.  
  4. import os
  5. import re
  6.  
  7. # vars for filenames
  8. filename = 'emaillist.txt'
  9. newfilename = 'emaillist-rev.txt'
  10.  
  11. # read file
  12. if os.path.exists(filename):
  13. data = open(filename,'r')
  14. bulkemails = data.read()
  15. else:
  16. print "File not found."
  17. raise SystemExit
  18.  
  19. # regex = [email protected]
  20. r = re.compile(r'(\b[\w.]+@+[\w.]+.+[\w.]\b)')
  21. results = r.findall(bulkemails)
  22.  
  23. emails = ""
  24. for x in results:
  25. emails += str(x)+"\n"
  26.  
  27. # function to write file
  28. def writefile():
  29. f = open(newfilename, 'w')
  30. f.write(emails)
  31. f.close()
  32. print "File written."
  33.  
  34. # function to handle overwrite question
  35. def overwrite_ok():
  36. response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No\n")
  37. if response == "Yes":
  38. writefile()
  39. elif response == "No":
  40. print "Aborted."
  41. else:
  42. print "Please enter Yes or No."
  43. overwrite_ok()
  44.  
  45. # write/overwrite
  46. if os.path.exists(newfilename):
  47. overwrite_ok()
  48. else:
  49. writefile()

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: chandchv on November 20, 2012

thank you ....

You need to login to post a comment.