2012-11-20 81 views
0

我試圖做到以下幾點,但數據最終是被feeded到前景messedup,請看看下面的鏈接pastie用於輸入,輸出和代碼1.4.3超鏈接到Outlook

1.read數據使用minidom命名 如果有超鏈接,2.and一個xml正與一個正則表達式替換添加AHREF屬性附加傷害 數據3.Outputting到Outlook

INPUT/Output:- 
http://pastie.org/5408694 

我的代碼: -

http://pastie.org/5408681

# Import package to access Outlook 
import win32com.client 
import re 
import xml.dom.minidom as minidom 
resultslis=[] 

def getsanityresults(xmlfile): 
    dom = minidom.parse(xmlfile) 
    data=dom.getElementsByTagName('Sanity_Results') 
    textnode = data[0].childNodes[0] 
    testresults=textnode.data 
    for line in testresults.splitlines(): 
    line = line.strip('\r,\n') 
    line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'<a href="\1">\1</a>', line) 
    print line  
    resultslis.append(line) 
    return resultslis 

def main(): 
    file = open('results.xml','r') 
    sanityresults=getsanityresults(file) 
    print sanityresults 
    msg_body=("<HTML><head></head>" 
     "<body> <font face = \"Calibri\" <br>" 
      "<font face = \"Calibri\"%s<br><br>" 

     "</body></html>" 
     ) % (sanityresults) 

    olMailItem = 0x0 
    obj = win32com.client.Dispatch("Outlook.Application") 
    newMail = obj.CreateItem(olMailItem) 
    newMail.HTMLBody = msg_body 
    newMail.display() 

if __name__ == '__main__': 
    main() 
+1

請複製這裏的代碼,否則當pastie頁面下降的問題不會爲未來的遊客感覺。 – BoppreH

+0

如果我粘貼這裏,縮進得到messedup ..如何解決它? – user1795998

+0

@user:具有四個空格的縮進代碼。 – Cameron

回答

0

我回答我自己question.following改變子程序工作

def getsanityresults(xmlfile): 
    testresult=[] 
    dom = minidom.parse(xmlfile) 
    data=dom.getElementsByTagName('Sanity_Results') 
    textnode = data[0].childNodes[0] 
    testresults=textnode.data 
    for line in testresults.splitlines(): 
     line = line.strip('\r,\n') 
     line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'<a href="\1">\1</a>', line) 
     testresult.append(line) 
    return '<br>'.join(testresult)