0

1.タグ「sanity_results」の間のxmlファイルを読み取ろうとしています(入力http://pastebin.com/p9H8GQt4を見てください)出力を印刷します

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)

if name == ' main ': main()

4

1 に答える 1

0

エラーメッセージを見てください:

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

そして を見てください: の結果をにmain()フィードしています。ただし、ファイル オブジェクトを返しますが、文字列であることが期待されます。open('results.xml', 'r')getsanityresultsopen(...)getsanityresultsxmlfile

intiの内容を抽出してフィードする必要があります。xmlfilegetsanityresults

ファイルの内容を取得するには、[Python ドキュメントのこの部分]9http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects) を参照してください。

特に、次を試してください。

xmlfile = open('results.xml', 'r')
contents = xmlfile.read() # <-- this is a string
testresults = getsanityresults(contents) # <-- feed a string into getsanityresults
# ... rest of code
于 2012-11-20T00:57:04.750 に答える