Python error xml.etree.ElementTree.ParseError: no element found: line 1, column 0

Asked 2 years ago, Updated 2 years ago, 53 views

I'd like to write the code as follows and save it as a text file, but what's the reason for that error? Sometimes I can, but sometimes I can't, so I have a headache.

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")

python xml

2022-09-20 15:14

1 Answers

if ".xml" and fileName in file:

This part is weird.

if '.xml': is always True.

Shouldn't it be like this if you rewrite the if conditional expression whether fileName exists in the file list and has '.xml'?

if fileName in file and '.xml' in fileName:


2022-09-20 15:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.