解析xml文件以创建超链接

2024-05-08 14:18:28 发布

您现在位置:Python中文网/ 问答频道 /正文

1.我试图读取标记“sanity\u results”(查看输入http://pastebin.com/p9H8GQt4)之间的xml文件并打印输出

2.对于包含http://或//的任何行或部分行,我希望它将“a href”超链接标记附加到链接,以便在我发布到电子邮件时,它们在电子邮件中显示为超链接

Input file(results.xml)
http://pastebin.com/p9H8GQt4


def getsanityresults(xmlfile):
    srstart=xmlfile.find('<Sanity_Results>')
    srend=xmlfile.find('</Sanity_Results>')
    sanity_results=xmlfile[srstart+16:srend].strip()
    sanity_results = sanity_results.replace('\n','<br>\n')
    return sanity_results

def main ():
xmlfile = open('results.xml','r')
contents = xmlfile.read()
testresults=getsanityresults(contents)
print testresults
for line in testresults:
        line = line.strip()
        //How to find if the line contains "http" or "\\" or "//" and append "a href"attribute
        resultslis.append(link)

如果名称=='main': 主()


Tags: 标记comhttpdeflinexmlfindresults
1条回答
网友
1楼 · 发布于 2024-05-08 14:18:28

请查看错误消息:

AttributeError: 'file' object has no attribute 'find'

然后看看main():您正在将open('results.xml', 'r')的结果输入getsanityresults。但是open(...)返回一个文件对象,而getsanityresults期望xmlfile是一个字符串。你知道吗

您需要提取xmlfile内容,并将其输入到getsanityresults。你知道吗

要获取文件的内容,请阅读[python文档的这一部分]9http://docs.python.org/2/tutorial/inputoutput.html#方法-文件对象)。你知道吗

尤其要尝试:

xmlfile = open('results.xml', 'r')
contents = xmlfile.read() # <  this is a string
testresults = getsanityresults(contents) # <  feed a string into getsanityresults
# ... rest of code

相关问题 更多 >