파이썬 XML파싱 질문있습니다..
글쓴이: Gamjago / 작성시간: 월, 2020/12/21 - 4:44오후
안녕하세요 PY4E라는 입문? 책을 보고 공부를 하고 있는데, XML파싱을 하려는데 계속 오류가 납니다.
어떻게 해야될지 전혀 감이 안오는데, 오류가 왜 나는 건가요?
일단은 코드는 이렇구요,
import xml.etree.ElementTree as ET from xml.etree.ElementTree import Element, dump, ElementTree try: tree = ET.parse('CIV4LeaderHeadInfos.xml') except: try: xm=input('Directory: ') except: print("Error: typed wrong directory.") quit() tree = ET.parse(xm) root = tree.getroot() ##print(root) ltag = root.findall("LeaderHeadInfo") print(ltag) print(ltag[0].find("Type").text)
이건 제가 파싱하려는 XML파일인데, Type부분의 텍스트를 출력하고 싶은데, ltag가 빈 리스트만 리턴하는데 어떻게 하면 될까요?
<?xml version="1.0"?> <!-- edited with XMLSPY v2004 rel. 2 U (<a href="http://www.xmlspy.com" rel="nofollow">http://www.xmlspy.com</a>) by Alex Mantzaris (Firaxis Games) --> <!-- Sid Meier's Civilization 4 --> <!-- Copyright Firaxis Games 2005 --> <!-- --> <!-- Leader Infos and AI Settings --> <Civ4LeaderHeadInfos xmlns="x-schema:CIV4CivilizationsSchema.xml"> <LeaderHeadInfos> <LeaderHeadInfo> <Type>LEADER_BARBARIAN</Type>
(~~~~생략~~~~)
실행결과는 이렇습니다.
[]
Traceback (most recent call last):
File "C:\Users\aaaa5\Desktop\Study\Python\LeaderXML.py", line 32, in
print(ltag[0].find("Type").text)
IndexError: list index out of range
File attachments:
첨부 | 파일 크기 |
---|---|
![]() | 1.65 MB |
Forums:
댓글 달기