从批量文本中提取所有电子邮件地址的python脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从批量文本中提取所有电子邮件地址的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. r = re.compile(r'([w.][email protected]+[w.]+.+[w.])')
  20. results = r.findall(bulkemails)
  21.  
  22. emails = ""
  23. for x in results:
  24. emails += str(x)+" "
  25.  
  26. # function to write file
  27. def writefile():
  28. f = open(newfilename, 'w')
  29. f.write(emails)
  30. f.close()
  31. print "File written."
  32.  
  33. # function to handle overwrite question
  34. def overwrite_ok():
  35. response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No ")
  36. if response == "Yes":
  37. writefile()
  38. elif response == "No":
  39. print "Aborted."
  40. else:
  41. print "Please enter Yes or No."
  42. overwrite_ok()
  43.  
  44. # write/overwrite
  45. if os.path.exists(newfilename):
  46. overwrite_ok()
  47. else:
  48. writefile()

以上是关于从批量文本中提取所有电子邮件地址的python脚本的主要内容,如果未能解决你的问题,请参考以下文章

从Txt,PDf,Google云端硬盘中的Doc文件中提取电子邮件地址

csharp 从文本中提取所有电子邮件地址

在 Python 中使用 BeautifulSoup 从脚本标签中提取文本

JavaScript 从批量文本中提取电子邮件(使用正则表达式,JavaScript和jQuery)

从批量文本中提取电子邮件(使用正则表达式JavaScript和jQuery)

Python批量提取docx格式Word文档中所有文本框内的文本