2016-12-02 93 views
0

我有一個腳本,用於從.txt文檔中導出所有電子郵件地址並打印所有電子郵件地址。 我想這個保存到LIST.TXT,如果可能的話刪除重複, 但它會給錯誤將打印輸出保存爲.txt

Traceback (most recent call last): 
    File "mail.py", line 44, in <module> 
    notepad.write(email.read()) 
AttributeError: 'str' object has no attribute 'read' 

腳本:

from optparse import OptionParser 
import os.path 
import re 

regex = re.compile(("([a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`" 
        "{|}~-]+)*(@|\sat\s)(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(\.|" 
        "\sdot\s))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)")) 

def file_to_str(filename): 
    """Returns the contents of filename as a string.""" 
    with open(filename) as f: 
     return f.read().lower() # Case is lowered to prevent regex mismatches. 

def get_emails(s): 
    """Returns an iterator of matched emails found in string s.""" 
    # Removing lines that start with '//' because the regular expression 
    # mistakenly matches patterns like 'http://[email protected]' as '//[email protected]'. 
    return (email[0] for email in re.findall(regex, s) if not  email[0].startswith('//')) 

if __name__ == '__main__': 
    parser = OptionParser(usage="Usage: python %prog [FILE]...") 
    # No options added yet. Add them here if you ever need them. 
    options, args = parser.parse_args() 

    if not args: 
     parser.print_usage() 
     exit(1) 

    for arg in args: 
     if os.path.isfile(arg): 
      for email in get_emails(file_to_str(arg)): 
       #print email 
       notepad = open("list.txt","wb") 
       notepad.write(email.read()) 
       notepad.close() 

     else: 
      print '"{}" is not a file.'.format(arg) 
      parser.print_usage() 
+3

試試這個:'notepad.write(電子郵件)'。只需電子郵件沒有.read() – neverwalkaloner

+0

就像@neverwalkaloner寫道,從'email.read()'刪除'.read()'。另外,爲了刪除重複項,你可以將set_get_emails(file_to_str(arg))):'中的'get_emails'的返回值轉換爲一個集合。 – internetional

+1

當我刪除.read()時,它顯示list.txt中只有1個電子郵件地址,當我使用打印電子郵件顯示幾百個。當提取繁忙時刷新list.txt時,電子郵件地址會發生變化,但它只顯示1. –

回答

0

When I remove .read() it shows only 1 email adres in list.txt when I use print email is shows a couple of hundred. when refreshing the list.txt while the extraction is busy the email adres change's but it only shows 1.

這是因爲你有open()和循環內的close(),即。即每個email都會重新寫入該文件,並且最終只寫入最後一個地址行。循環更改爲:

  notepad = open("list.txt", "wb") 
      for email in get_emails(file_to_str(arg)): 
       #print email 
       notepad.write(email) 
      notepad.close() 

甚至更​​好:

  with open("list.txt", "wb") as notepad: 
       for email in get_emails(file_to_str(arg)): 
       #print email 
       notepad.write(email)