파이썬 오류 xml.etree.ElementTree.ParseError: no element found: line 1, column 0

조회수 1538회

다음과 같이 코드를 작성해서 텍스트 파일로 저장하려고 하는데, 저 오류가 떴다가 안 떴다가 하는데 이유가 뭔가요? 어떨 때는 되고 어떨 때는 안 돼서 머리 아프네요.

def parsingXml(fileName):

    import xml.etree.ElementTree as et
    import sys
    import os
    import logging
    logging.basicConfig(format = "%(asctime)s, %(levelname)s: %(message)s", datefmt = "%Y-%m-%d %H:%M:%S %p", level = logging.DEBUG)


    try:
        sys.stdout = open("C:\\btobl_xml\\{}.txt".format(fileName), "w", encoding = "utf8")

    except Exception:
        logging.exception("OUT ERROR")


    try:
        targetDir = "C:\\btobl_xml"
        fileLIst = os.listdir(targetDir)
        xmlList = []

        for file in fileLIst:
            if ".xml" and fileName in file:
                xmlList.append(file)

        for xmlFile in xmlList:
            targetPath = targetDir + "\\" + xmlFile
            root = et.parse(targetPath).getroot()

            for tag1 in root:
                print("-" * 50)
                print("->", tag1.tag, ":", tag1.text)
                for tag2 in tag1:
                    print("-->", tag2.tag, ":", tag2.text, tag2.attrib)
                    for tag3 in tag2:
                        print("--->", tag3.tag, ":", tag3.text)
                        for tag4 in tag3:
                            print("---->", tag4.tag, tag4.text, tag4.attrib)


    except Exception:
        logging.exception("parsing error")
  • 에러날때 targetPath 출력해보세요 초보자 2021.8.17 15:25

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)