Python minidom不需要的空格

问题描述 投票:0回答:3

我正在使用Python将数据写入.xml文件。我有一个名为statistics.xml的文件,每当我调用我的方法“ writeIntoXml()”时,它应将数据添加到该统计信息的xml文件中。现在,Python可以完美地做到这一点,唯一的问题是,在我向文件中写入新数据之前,它在文件中的所有元素之间添加了不需要的空格。像这样:

<AantalTicketsPerUur>
    <Dag datum="2012-03-16">
        <Aantal_tickets Aantal="24" uurinterval="0u-1u"/>
        <Aantal_tickets Aantal="68" uurinterval="1u-2u"/>
        <Aantal_tickets Aantal="112" uurinterval="2u-3u"/>
        <Aantal_tickets Aantal="98" uurinterval="3u-4u"/>
    </Dag>
</AantalTicketsPerUur>

成为这个(中间没有空格的元素是新数据):

<AantalTicketsPerUur>


    <Dag datum="2012-03-16">


        <Aantal_tickets Aantal="24" uurinterval="0u-1u"/>


        <Aantal_tickets Aantal="68" uurinterval="1u-2u"/>


        <Aantal_tickets Aantal="112" uurinterval="2u-3u"/>


        <Aantal_tickets Aantal="98" uurinterval="3u-4u"/>


    </Dag>


    <Dag datum="2012-03-16">
        <Aantal_tickets Aantal="24" uurinterval="0u-1u"/>
        <Aantal_tickets Aantal="68" uurinterval="1u-2u"/>
        <Aantal_tickets Aantal="112" uurinterval="2u-3u"/>
        <Aantal_tickets Aantal="98" uurinterval="3u-4u"/>
    </Dag>
</AantalTicketsPerUur>

我该如何解决?注意:我确实使用.toprettyxml()方法

提前感谢

python xml parsing minidom
3个回答
1
投票

您可能想使用toxml而不是不会修改格式的toprettyxml:

def write_xml(filename, dom):
    f = open(filename, "w")
    f.write(dom.toxml("utf-8"))
    f.close()

0
投票

我同意qgi的回答。但是请注意,关于根元素之外的注释,这两种方法似乎存在相反的怪癖。例如,如果我使用minidom解析此XML文件...

<?xml version="1.0" encoding="utf-8"?>

<!-- testing 1 -->
<!-- testing 2 -->

<sources autodelete="false" syncmedia="true" multivalue_separator=";; ">

    <!-- testing 3 -->
    <source 
        id_field="Lex GUID"
        source_audio_folder="samples/audio"
        source_image_folder="samples/pictures" >
        <source_field anki_field="Lex GUID" />
  </source>

    <!-- Test blah blah
        blah blah 
        blah 
    -->
    <source 
        id_field="Example"  
        source_audio_folder="samples/audio"
        source_image_folder="samples/pictures" >
        <source_field anki_field="Example" />

    </source>

</sources>

<!-- test THE END -->

...然后将其保存为两个不同的文件,toxml很好地保留了这些外部部分(但根目录内没有保留),而toprettyxml仅保留了根目录内的那些部分。我正在使用Python 2.7 BTW。这是tmp1.xml('pretty'):

<?xml version="1.0" encoding="utf-8"?><!-- testing 1 --><!-- testing 2 --><sources autodelete="false" multivalue_separator=";; " syncmedia="true">

    <!-- testing 3 -->
    <source id_field="Lex GUID" source_audio_folder="samples/audio" source_image_folder="samples/pictures">
        <source_field anki_field="Lex GUID"/>
  </source>

    <!-- Test blah blah
        blah blah 
        blah 
    -->
    <source id_field="Example" source_audio_folder="samples/audio" source_image_folder="samples/pictures">
        <source_field anki_field="Example"/>

    </source>

</sources><!-- test THE END -->

...这是tmp2.xml(纯字符串):

<?xml version="1.0" encoding="utf-8"?>
<!-- testing 1 -->
<!-- testing 2 -->
<sources autodelete="false" multivalue_separator=";; " syncmedia="true">



    <!-- testing 3 -->


    <source id_field="Lex GUID" source_audio_folder="samples/audio" source_image_folder="samples/pictures">


        <source_field anki_field="Lex GUID"/>


    </source>



    <!-- Test blah blah
        blah blah 
        blah 
    -->


    <source id_field="Example" source_audio_folder="samples/audio" source_image_folder="samples/pictures">


        <source_field anki_field="Example"/>



    </source>



</sources>
<!-- test THE END -->

以防万一,这是产生那些代码的Python代码:

import xml.dom.minidom as minidom
tree = minidom.parse(file_path)
s1 = tree.toxml('utf-8')
s2 = tree.toprettyxml('    ', '\n', 'utf-8')
with open ('tmp1.xml', mode='w') as outfile:  # Python 3 would also allow: encoding='utf-8'
    outfile.write(s1.encode('utf-8'))
with open ('tmp2.xml', mode='w') as outfile:
    outfile.write(s2.encode('utf-8'))

0
投票

这是一个Python3解决方案,它摆脱了丑陋的换行符问题(大量的空白),并且仅使用标准库,与大多数其他实现不同。

import xml.etree.ElementTree as ET
import xml.dom.minidom
import os

def pretty_print_xml_given_root(root, output_xml):
    """
    Useful for when you are editing xml data on the fly
    """
    xml_string = xml.dom.minidom.parseString(ET.tostring(root)).toprettyxml()
    xml_string = os.linesep.join([s for s in xml_string.splitlines() if s.strip()]) # remove the weird newline issue
    with open(output_xml, "w") as file_out:
        file_out.write(xml_string)

def pretty_print_xml_given_file(input_xml, output_xml):
    """
    Useful for when you want to reformat an already existing xml file
    """
    tree = ET.parse(input_xml)
    root = tree.getroot()
    pretty_print_xml_given_root(root, output_xml)

我发现了如何解决常见的换行问题here

© www.soinside.com 2019 - 2024. All rights reserved.