2017-08-30 6 views
1

メーリングリストの束を変更したい。各メーリングリストには、「古い」アドレスと呼ばれる電子メールアドレスのリスト(1行に1つ)が含まれています。特定の電子メールアドレスについては、古いものが.xlsxファイル内で新しいものとともに参照されます。古いアドレスが参照されていない場合は、古いアドレスが削除されていることを意味します。メーリングリストの電子メールアドレスが既に良いものになっていることがあります。この場合は、変更しないでください。Excelファイルに応じてメーリングリストを再フォーマットする

私はPythonでそれを行いました。私は本当に問題はありませんが、私は自分の仕事を分かち合おうと思っていたので、それほど明白ではないことに気付きました。まず、すでに見た記事のように見えるので、参考になるかもしれません。私のコードは絶対に最適化されていないので(私の場合は〜0.5秒かかるので最適化する必要はありませんでした)、私は10の場合に自分のコードを最適化するために何をするのが好きか分かります^ 8メーリングリスト。ここ

答えて

0

は私が最終的に実装されたPythonコードです:

import xlrd 
import os 
path_old = 'toto' 
path_new = 'tata' 
mailing_lists = os.listdir(path_old) 
good_domain = 'gooddomain.fr' 
printing_level = 3 

# reading of the excel file 
xlsfilename = 'adresses.xlsx' 
xlsfile = xlrd.open_workbook(xlsfilename) 
number_of_persons = 250 
number_column_old_mail = 7 
number_column_new_mail = 5 
newmail = [] 
oldmail = [] 
for count in range(number_of_persons): 
    oldmail.append(xlsfile.sheets()[0].cell(count,number_column_old_mail).value) 
    newmail.append(xlsfile.sheets()[0].cell(count,number_column_new_mail).value) 
############ 

for mailinglist_name in mailing_lists: 
    if printing_level > 0: 
     print('* dealing with mailing list ',mailinglist_name) 
    new_mailinglist = [] 
    new_name = mailinglist_name + '_new' 

    with open(path_old+'/'+mailinglist_name,'r') as inputfile: 
     for line in inputfile: 
      if len(line)<2: # to ignore blank lines. This length of 2 is completly arbitrary 
       continue 
      line = line.rstrip('\n') 
      ok = False 

# case 1: the address inside the old mailing list is ok ==> copied in the new mailing list 
      if '@' in line: 
       if line[line.index('@')+1:] == good_domain: 
        new_mailinglist.append(line) 
        if printing_level > 1: 
         print(' --> address ',line,' already ok ==> kept unmodified') 
        ok = True 

# case 2: the address inside the old mailing list is not ok ==> must be treated 
      if not ok: 
       if printing_level > 1: 
         print(' --> old address ',line,' must be treated') 
       try: 
# case 2a: the old address is in the excel file ==> replaced 
        ind = oldmail.index(line) 
        if printing_level > 2: 
         print(' --> old address found in the excel file and replaced by ',newmail[ind]) 
        new_mailinglist.append(newmail[ind]) 
       except ValueError: 
# case 2b: the old address is obsolete ==> removed 
        if printing_level > 2: 
         print(' --> old address removed') 

    with open(path_new+'/'+new_name,'w') as outputfile: 
     for address in new_mailinglist: 
      outputfile.write(address+'\n') 
関連する問題