错误:“在字符串格式设置期间未转换所有参数”

Error: "not all arguments converted during string formatting"

提问人:Van Ludius Corona 提问时间:2/15/2023 最后编辑:mokenVan Ludius Corona 更新时间:2/15/2023 访问量:46

问:

我有以下脚本,它将比较两个文件夹之间的 xml 文件并将一些 none 类型参数转换为“”。

问题是,在文件检查的某个点,它会返回“在字符串格式化期间未转换所有参数”的错误 有没有办法解决或绕过这部分。

我有以下脚本:

import sys
from pandas.errors import ParserError


def compare_xml_files1(location1, location2):
    skip_attributes = ['href', 'id', 'ImportGUID', 'ItemSignature']
   
    try:

        with zipfile.ZipFile(location1, 'r') as location1_zip:
            with zipfile.ZipFile(location2, 'r') as location2_zip:
                xml_files = [f for f in location1_zip.namelist() if
                             f.endswith((".dita", ".ditamap", ".customerproperties"))]
                flag = 0
                for xml_file in xml_files:
                    flag1 = 0
                    filename = os.path.basename(xml_file)
                    try:
                        location1_xml = location1_zip.read(xml_file).decode()
                        # location1_xml = location1_zip.open(xml_file)
                        location2_xml = location2_zip.read(xml_file).decode()
                        # location2_xml = location2_zip.open(xml_file)
                    except KeyError:
                        LOGGER.warning(f"{filename} not found in Golden Source")
                        flag += 1
                        continue
                    location1_root = ET.fromstring(location1_xml)
                    location2_root = ET.fromstring(location2_xml)
                    # location1_root = ET.ElementTree(file=location1_xml)
                    # location2_root = ET.ElementTree(file=location2_xml)
                    LOGGER.info(f"Validating dita file: {filename}")
                    for elem1, elem2 in zip(location1_root.iter(), location2_root.iter()):
                        if elem1.tag == elem2.tag:
                            if (all(n in elem2.attrib.keys() for n in elem1.attrib.keys()) and all(
                                    n in elem1.attrib.keys() for n in elem2.attrib.keys())):
                                for key in elem1.attrib:
                                    if key in skip_attributes:
                                        pass  # skipping check for attributes list
                                    else:
                                        if elem1.attrib[key] == elem2.attrib[key]:
                                            pass
                                        else:
                                            flag += 1
                                            flag1 += 1
                                            LOGGER.warning('Dita file content mismatched at attribute   level: ' + key)
                                            LOGGER.warning('Dita file content mismatched for Tag value: ' + elem1.tag)
                                            LOGGER.warning('Source Data mismatched attribute: ' + elem1.attrib[key])
                                            LOGGER.warning(
                                                'Golden Data mismatched attribute: ' + elem2.attrib[key] + "\n")
                            else:
                                flag += 1
                                flag1 += 1
                                LOGGER.error('Attributes are missing at tag: ' + elem1.tag)
                                LOGGER.error('Attributes in  Source file:')
                                LOGGER.error(elem1.attrib.keys())
                                LOGGER.error('Attributes in  Golden file:')
                                LOGGER.error(elem2.attrib.keys())
                                print("\n")

                            if type(elem1.text) == str and date_validate(elem1.text) is False:
                                if elem1.text == elem2.text:
                                    pass

                                else:
                                    flag += 1
                                    flag1 += 1
                                    if elem2.text is None:  # to convert Nonetype to empty string
                                        elem2.text = ""

                                    LOGGER.warning('Dita file content mismatched for Tag value: ' + elem1.tag)
                                    # LOGGER.warning('Source Data mismatched text: ' + elem1.text)
                                    LOGGER.warning(f'Source Data mismatched text: {elem1.text}')
                                    # LOGGER.warning('Golden Data mismatched text: ' + elem2.text + "\n")
                                    LOGGER.warning(f'Golden Data mismatched text: {elem2.text}\n')
                        else:  # to convert Nonetype to empty string
                            flag += 1
                            flag1 += 1
                            if elem2.text is None:
                                elem2.text = ""
                            LOGGER.warning('Dita file content mismatched at Tag value')
                            # LOGGER.warning('Source Data mismatched text: ' + elem1.text)
                            LOGGER.warning(f'Source Data mismatched text: {elem1.text}')
                            # LOGGER.warning('Golden Data mismatched text: ' + elem2.text + "\n")
                            LOGGER.warning(f'Golden Data mismatched text: {elem2.text}\n')

                    if flag1 == 0:
                        LOGGER.info('%s Dita file content matched\n', filename)
                        print('*' * 100)
                    else:
                        LOGGER.error('%s Dita file content does not matched\n', filename)
                        LOGGER.info('File Location :', location1, '\n')
                        print('*' * 100)
                if flag == 0:
                    return True
                else:
                    LOGGER.error('Job has an error')
                    return False

    except Exception as e:  
        LOGGER.error(str(e))
        sys.exit(1)

    except ParserError as e:
        filename, line_no, function_name = e.__traceback__.tb_frame.f_code.co_filename, e.__traceback__.tb_lineno, \
                                           e.__traceback__.tb_frame.f_code.co_name
        error_message = e.args[0]

        LOGGER.error(f'ParseError: {error_message}')
        LOGGER.debug({'filename': filename, 'lineno': line_no, 'name': function_name, 'type': 'ParserError',
                      'message': error_message})
        return False

    except KeyError as e:
        filename, line_no, function_name = e.__traceback__.tb_frame.f_code.co_filename, e.__traceback__.tb_lineno, \
                                           e.__traceback__.tb_frame.f_code.co_name
        error_message = e.args[0]

        LOGGER.error('KeyError' + str(e))
        LOGGER.debug({'filename': filename, 'lineno': line_no, 'name': function_name, 'type': 'KeyError',
                      'message': error_message})

        return False
python-3.x xml 解析

评论


答: 暂无答案