Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解析xml文件以创建超链接_Python - Fatal编程技术网

Python 解析xml文件以创建超链接

Python 解析xml文件以创建超链接,python,Python,1.我试图读取标记“sanity_results”(查看输入)和打印输出之间的xml文件 2.对于包含http://或//的任何行或部分行,我希望它在链接中附加“a href”超链接标记,以便在我发布到电子邮件时,它们在电子邮件中显示为超链接 Input file(results.xml) http://pastebin.com/p9H8GQt4 def getsanityresults(xmlfile): srstart=xmlfile.find('<Sanity_Resul

1.我试图读取标记“sanity_results”(查看输入)和打印输出之间的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)
输入文件(results.xml)
http://pastebin.com/p9H8GQt4
def getsanityresults(XML文件):
srstart=xmlfile.find(“”)
srend=xmlfile.find(“”)
sanity_results=xmlfile[srstart+16:srend].strip()
健全结果=健全结果。替换('\n','
\n') 返回健全的结果 defmain(): xmlfile=open('results.xml','r') contents=xmlfile.read() testresults=getsanityresults(目录) 打印测试结果 对于测试结果中的行: line=line.strip() //如何查找该行是否包含“http”或“\\”或“/”并附加“a href”属性 resultslis.append(链接)
如果name='main':
main()

查看您的错误消息:

AttributeError: 'file' object has no attribute 'find'
然后看一下
main()
:您正在将
open('results.xml','r')
的结果输入
getsanityresults
。但是
open(…)
返回一个file对象,而
getsanityresults
期望
xmlfile
是一个字符串

您需要提取
xmlfile
的内容,并向inti
getsanityresults
提供数据

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

特别是,请尝试:

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

contents=xmlfile.read()#coffee-谢谢,我的问题#2,如何查找行中是否包含“http”或“\\”或“/”并附加“a href”属性您最好在单独的问题中询问单独的问题。如果每个链接都在自己的行上,您可以使用查看该行中是否存在
http://
\`链接,如果存在,请用
`环绕链接。