2016-08-23 1 views
-2
for tm in teamtree.iter('team_members'): 

위의 기능을 사용하여 CSV에 이러한 필드를 출력하려고합니다. xml 데이터는 (projectDetJoined)라는 변수에 저장됩니다.xml to CSV - AttributeError : 'NoneType'객체에 'text'속성이 없습니다.

이 오류가 발생합니다.

Traceback (most recent call last): 
    File "10Other.py", line 481, in <module> 
parseXMLTaskDetails() 
    File "10Other.py", line 355, in parseXMLTaskDetails 
taskcid = (t.find('cid').text) 
AttributeError: 'NoneType' object has no attribute 'text' 

항목이 xml 데이터에 있습니다.

그 이유는 무엇입니까? 나는 같은 방식으로 구조화되어 있지만 작동하는 유사한 기능을 가지고 있습니다.

+1

XML에서 'team_members' 요소에는'cid'와 같은 하위 요소가 없으며'item' 하위 요소가 있습니다. 아마도 당신은 팀 트리에서'tm을 의미했을 것입니다 .iterfind ('team_members/item')'. CSV 헤더가 일부 항목에 대해 대소 문자를 달리하지 않았다면 for 루프 본문에'tm.findtext'를 매핑하여 쓰기 값을 추출 할 수있었습니다. 추가를 위해 항상 파일을 다시 열지 말고 XML 추출을 파일과 csv-writer를 처음 생성하는 with-block으로 이동하십시오. 마지막'csvfile.close()'도 중복됩니다. –

+0

고맙습니다. 그게 효과가 있었어. 제안에 따라 코드를 최적화했습니다. 고마워. iterfind가 지금이었던 것을 몰랐다. 왜 질문에 대한 아래 표를하는지 잘 모르겠다. 그러나 어쨌든, 이것은 잘 돌아 갔고 이제는 알고 있습니다. 다시 한번 감사드립니다. –

답변

0

Ilja Everilä의 의견이 내 문제를 해결했습니다.

In your XML the team_members element does not have subelements like cid etc. It has item subelements. Perhaps you meant for tm in teamtree.iterfind('team_members/item'). If your CSV headers didn't have different case for some items, you could've just mapped tm.findtext over them in the for-loop body to extract the values for writing. Don't reopen the file all the time for append, but move the XML extraction to the with-block that initially creates the file and csv-writer. The final csvfile.close() is also redundant.

관련 문제