1

I'm very, very new to Python and I figured I could do a simple app to parse an xml format for me. The problem is, it almost looks like I'm getting invalid elements from the element.getElementsByTagName() method. I'll attach all related source code below.

The problem arises when trying to format the data that I capture from the XML; the error code is as follows:

Traceback (most recent call last):
        File "C:/development/xml2dnf/main.py", line 8, in <module> output_file.write(str(result))
        File "C:\development\xml2dnf\Shotsheet.py", line 18, in __str__ reply += str(shot) + "\n"
        File "C:\development\xml2dnf\StaticShot.py", line 19, in __str__ self.rect[2], self.rect[3])
ValueError: unexpected '{' in field name

I attempted to fiddle around with the reported problematic line until I figured that the error might not be there. As I went up my hierarchy, I noticed that if I tell one of my classes to not parse a specific list I send to it, it works almost as expected. When I don't parse the aformentioned, the error magically goes away.

Thinking that a pair of classes of mine were the problem, I went to them and did all I could to locate an alleviate that stray '{'. I found no problems.

I tried to troubleshoot, going up until the xml itself. I printed it and it looked okay. However, when I printed the results I should have gotten, I noticed that it prints some strange, invalid values and then prints what I should have gotten should the error not be present.

I did some Google searches, but I could find very little on this issue. Whatever I could find either didn't alleviate my problem or was inconsequential. I've exhausted my troubleshooting, considering also that I'm new to the language.


UPDATE:

After messing around with XML, I noticed that the invalid elements were a result of me not understanding how getElementsById works. The method returns all elements with the ID as opposed to just those who are direct children of the element. Tweaking the implementation solved the issue.

Now that I got that out of the way, I'm still getting the aforementioned error. The only thing I can think of is that my __str__ implementation for my classes is faulty, but I still fail to see how. Printing every single parameter on its own yields the expected results, but using __str__ fails miserably and the error helps me not in any conceivable way.

For the sake of illustration, here's the code the interpreter says is at fault:

def __str__(self):
        reply = "ShotData\x7Bid = {0} rect = ({1}, {2}, {3}, {4}) ".format(self.index, self.rect[0], self.rect[1],
                                                                           self.rect[2], self.rect[3])
        if self.render != 'ALPHA':
            reply += "render = {0} ".format(self.render)

        if self.collision >= 0:
            reply += "collision = {0} ".format(self.collision)

        if self.fixed_angle:
            reply += "fixed_angle = true angular_velocity = {0}\x7D".format(self.angular_velocity)

        delay_string = str(self.delay_color)
        reply += "delay_color = {0} ".format(delay_string)

        return reply

The code that calls it:

def __str__(self):
        reply = "#UserShotData\n\nshot_image = {0}\ndelay_rect=({1}, {2}, {3}, {4})\n\n"

        for shot in self.shot_array:
            reply += str(shot) + "\n"

        delay_x1 = self.delay_rect[0]
        delay_y1 = self.delay_rect[1]
        delay_x2 = self.delay_rect[2]
        delay_y2 = self.delay_rect[3]

        return reply.format(self.shot_image, delay_x1, delay_y1, delay_x2, delay_y2)

And the instruction that asks for the string representation:

output_file.write(str(result))

As always, any help on this issue would be greatly appreciated.

2
  • 2
    There is too much code here. Can you reduce it to a minimal example that demonstrates the problem? Commented Feb 11, 2016 at 21:27
  • @larsks I have promptly updated the question as the nature of the error has shifted. I also minimized the amount of code shown to a bare minimum. Commented Feb 16, 2016 at 15:05

2 Answers 2

2

The problem is that writing ShotData\x7Bid is exactly the same as writing ShotData{Bid, using an escape sequence here doesn't cause .format to treat the curly brace any different. If you want to escape a curly brace, you need to double it:

reply = "ShotData{{id = {0} rect = ({1}, {2}, {3}, {4}) ".format(self.index, self.rect[0], self.rect[1], self.rect[2], self.rect[3])
Sign up to request clarification or add additional context in comments.

Comments

0

The answer lies in the format line. The \x7B character breaks it. If you want to insert curly brackets in a formatted string, I suggest you do it afterwards :

 reply = "{0} rect = ({1}, {2}, {3}, {4})   ".format(self.index, self.rect[0], self.rect[1], self.rect[2],self.rect[3])
 reply = "ShotData{Bid = " + reply

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.