2012-11-20 3 views
0

내가 xml(productline)에 함수 호출이 될 때마다 followinge 오류가 계속,하지만 난 file = open('config\\' + productLine + '.xml','r')로 functioncall를 교체하는 경우, 작동하는 것 같다 \ , 왜? 없습니다 해당 파일이나 디렉토리 : 'N

def xml(productLine): 
     with open('config\\' + productLine + '.xml','r') as f: 
    return f.read() 


def getsanityresults(productline): 

xmlfile=xml(productline) // replace with file = open('config\\' + productLine + '.xml','r') 
dom = minidom.parse(xmlfile) 
data=dom.getElementsByTagName('Sanity_Results') 
#print "DATA" 
#print data 
textnode = data[0].childNodes[0] 
testresults=textnode.data 
#print testresults 
for line in testresults.splitlines(): 
    #print line 
    line = line.strip('\r,\n') 
    #print line 
    line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'<a href="\1">\1</a>', line) 
    print line 
    #print line  
    resultslis.append(line) 
print resultslis 
return resultslis 

오류 : 당신은 미묘하게 다른 이름으로, 두 개의 변수를 가지고

Traceback (most recent call last): 
File "C:\Dropbox\scripts\announce_build_wcn\wcnbuild_release.py", line 910, in <module> 
    main() 
File "C:\Dropbox\scripts\announce_build_wcn\wcnbuild_release.py", line 858, in main 
testresults=getsanityresults(pL) 
File "C:\Dropbox\scripts\announce_build_wcn\wcnbuild_release.py", line 733, in getsanityresults 
    dom = minidom.parse(xmlfile) 
File "C:\python2.7.3\lib\xml\dom\minidom.py", line 1920, in parse 
    return expatbuilder.parse(file) 
File "C:\python2.7.3\lib\xml\dom\expatbuilder.py", line 922, in parse 
fp = open(file, 'rb') 
IOError: [Errno 2] No such file or directory: '<root>\n <PL name = "MSM8930.LA.2.0-PMIC-8917">\n 
+0

이 코드는 실행되지 않습니다. 들여 쓰기를 올바르게하십시오. –

+0

함수가 파일 객체가 아닌 문자열을 반환합니다 ... 미니 도큐멘트를 보지 않았지만 중요 할 수 있습니다. – mgilson

+0

@mgilson - 어떻게 해결할 수 있습니까? – user1795998

답변

1

minidom.parse() 매개 변수로 파일 이름이나 파일 객체 중 하나를 기대하지만, 당신이 파일의 내용을 전달하는이 시도 :

import os 
from xml.dom import minidom 

doc = minidom.parse(os.path.join('config', productline + '.xml')) 

당신이 minidom을 선호 특정 요구 사항이 없다면, xml.etree.cElementTree을 사용하여 파이썬에서 xml로 작업하십시오. 좀 더 복잡한 경우에 당신이 필요할 수도있는 더 많은 파이썬과 lxml은 API를 지원하므로 두 번 배울 필요가 없습니다.

1

I replace the functioncall with file = open('config\\' + productLine + '.xml','r') , it seems to work, why?

:

xmlfile=xml(productline) // replace with file = open('config\\' + productLine + '.xml','r') 

있다 productline (소문자 l)와 productLine (대문자 L) .

두 경우 모두 동일한 변수를 사용하면보다 일관된 결과를 얻을 수 있습니다.

+0

아니오, 저에게 어떤 differene도하지 마십시오 – user1795998

관련 문제