Python xml to txt conversion script problem

Hi,

I have a Python script that converts a bunch of XML files from one machine learning label format to another.

The script works as long as there are more than one .+, but if there is only one it fails.

I suspect the culprit is the for loop in line 48, but I can't seem to find a way to fix it.

Pic related is the for loop in the script.

This is the script:

github.com/krustnic/xml2kitti

Attached: for_loop.png (679x274, 14K)

This is an example xml file that works.

Attached: Works.png (402x626, 26K)

This is an example xml file that fails.

Attached: Fails.png (376x469, 20K)

This is the error I get once the script encounters an xml file with only one object.

If anyone knows what's up and could help nudge me in the right direction I'd be really grateful.

Attached: error.png (580x114, 7K)

BUMP

Why are you using :-1 for objects?

>Why are you using :-1 for objects?

That's what the author of the script put there. Changing it doesen't work though, if I change it to 1 it only process one object.

Do you know what :-1 means?

>file.write(self.fill_template(object)) using loop variable
why

Not in this context, no. I assume it means to run it until there is only one match left or something along those lines? Changing it to one makes it only process the first object regardless of how many is in the file. Then line 51 for some reason also runs, so in xml files with only one object it spits out two lines..

This works though. I removed any number from the for loop and deleted line 51. Although now it also include a newline in the converted output txt when the xml file only have one object. I don't know if this matters though, I'll have to try!

Attached: Works2.png (677x271, 13K)

I just perused the test data the original author put up and it obviously wasn't properly checked by the author because there's a clear error in the output.

Change line 51 to be the following:

file.write(self.fill_template(objects[-1]))

It looks like the code basically loops over all objects, doing the template translation. For all object EXCEPT the last object in the list, they also want to append a new line character (last object will have an EOF appended when the file is closed).

This is suspiciously bad code, but I be the above will make it work.

>because there's a clear error in the output.

What kind of error is that?

Is it that the label is put in as a string and not a number? I'm not familiar with the KITTI format.

The :-1 means all the elements in the array except the last one.
When you have one element in the array it returns a zero length slice so it can't fill the object variable.
Worse, though, when the length of the array is greater than one it'll loop the length of the entire array, but the final iteration will use the previous iterations data because there's no element in the slice that coincides with that iteration.
So if you look in the test text files in that github you'll find the last two lines are identical.

you are trying to call object before giving it a value

>Change line 51 to be the following:
>file.write(self.fill_template(objects[-1]))

That seems to work, no extra newline in the output either! Thanks!

That makes sense, you're right. This seems to take care of that, I tried it on the test data it seems to spit out correct output.

I'm gonna go try this out now, thanks for the help anons. I've learned something new today, which is always great. :)

objects[:-1] is an empty list if objects is length 1. [:-1] means all but the last element.

>Making such a simple mistake

Attached: 1535081033368.jpg (1294x478, 115K)

>Python
Jeez what an ugly language

OP's code is not pythonic