在一个lin中关闭xml标记

2024-10-01 17:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

循环遍历列表

                for file in files:
                    if id == file['param_id']:
                        resources_dict = {'fileNo': str(i), 'startPageNo': str(i), 'endPageNo': str(i),
                                          'format': 'cpk:JPEG'}

                        ET.SubElement(cpf_resources, 'cpf:ContentFile', resources_dict).text = 'cid:{}'.format(str(file['filename']))
                        i = i + 1

然后将数据写入文件,如下所示:

 tree = ET.ElementTree(jobticket)
        filename = '{}\\{}.xml'.format(os.getcwd(), get_af_value(project_data, id, 'filename'))
        tree.write(filename, encoding="UTF-8", xml_declaration=True)

在结束文件中,数据显示如下:

 <cpf:Resources>
        <cpf:ContentFile endPageNo="1" fileNo="1" format="cpk:JPEG" startPageNo="1">cid:page_0005.jpg
        </cpf:ContentFile>
        <cpf:ContentFile endPageNo="2" fileNo="2" format="cpk:JPEG" startPageNo="2">cid:page_0009.jpg
        </cpf:ContentFile>
    </cpf:Resources>

有没有办法在同一行中显示标记</cpf:ContentFile>的结束?你知道吗

<cpf:ContentFile endPageNo="2" fileNo="2" format="cpk:JPEG" startPageNo="2">cid:page_0009.jpg</cpf:ContentFile>

Tags: idformatpagefilenamefilejpegresourcescid
1条回答
网友
1楼 · 发布于 2024-10-01 17:33:41

在几次诅咒之后,我成功地创造了这样的东西:

        tree = tree.getroot()
        tree = ET.tostring(tree)
        xmlstr = minidom.parseString(tree).toprettyxml(indent = "   ", encoding='UTF-8')
        with open("filename.xml", "w") as f:
           f.write(xmlstr)

也许有人可以用。你知道吗

相关问题 更多 >

    热门问题