我正在尝试执行以下操作,但最终获取前景的数据是messedup,请查看下面的pastie链接以获取输入,输出和代码
1.使用minidom从xml读取数据 2.如果有超链接,则用正则表达式替换以添加ahref属性 3.将数据输出到展望
INPUT/Output:-
http://pastie.org/5408694
我的代码: -
# 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()
答案 0 :(得分:1)
我正在回答我自己的问题。接下来对子程序进行了改动
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)