option end="" in python print() function does not work consistently

316 Views Asked by At

The following python code:

import xml.sax

class C_Handler(xml.sax.ContentHandler):

    def startDocument(self):
        print("<html><head><title>Lieferungen</title></head>")
        print("<body><h1>Lieferungen</h1><hr>\n")
        print("<table border=\"1\"><tr><th>Nummer</th><th>Artikel</th><th>preis</th><th>Lieferant</th></tr>\n")


    def startElement(self, tag, attributes): 
        if tag == "artikel":
            print("<tr><td>{}</td> <td>".format(attributes["id"]),end="")
        if tag == "preis":
            print("</td> <td>", end="")
        if tag == "lieferant":
            print("</td> <td>", end="")

    def endElement(self, tag):
        if tag == "lieferant":
            print("</td> </tr>")

    def characters(self, content):
        print("{}".format(content), end="")

    def endDocument(self):
        print("\n</table>\n</body>\n</html>\n")

if ( __name__ == "__main__"):

    c = C_Handler()

    xml.sax.parse("lieferungen.xml", c)

is supposed to transform the following xml-file:

<?xml version="1.0"?>
<lieferungen>
     <artikel id="3526">
         <name>apfel</name>
         <preis stueckpreis="true">8.97</preis>
         <lieferant>Fa. Krause</lieferant>
     </artikel>
</lieferungen>

into the following output:

<html><head><title>Lieferungen</title></head>
<body><h1>Lieferungen</h1><hr>

<table border="1"><tr><th>Nummer</th><th>Artikel</th><th>preis</th><th>Lieferant</th></tr>

<tr><td>3526</td> <td> apfel </td> <td> 8.97 </td> <td> Fa. Krause </td> </tr>

</table>
</body>
</html>

However, what I get is this:

<html><head><title>Lieferungen</title></head>
<body><h1>Lieferungen</h1><hr>

<table border="1"><tr><th>Nummer</th><th>Artikel</th><th>preis</th>   <th>Lieferant</th></tr>


     <tr><td>3526</td> <td> 
        apfel  
       </td> <td> 8.97  
       </td> <td> Fa. Krause </td> </tr>

</table>
</body>
</html>

In other words: the end="" option in the print function does not work as expected. What is strange is this: Sometimes it works (after "Fa. Krause") and in other cases (e.g. after "apfel") it does not work. Since both "Fa. Krause" and "apfel" are character data, the characters() method is applied in each case by the content handler. Still, the result is not the same, which makes this whole thing extremely weird.

1

There are 1 best solutions below

0
On BEST ANSWER

It appears that the parser calls characters() with the whitespace between the tags, and that whitespace contains newlines, which get printed in places that the expected output does not match.

Removing all whitespace between the tags in the sample xml document makes the actual output match the expected.