Hi,

I'm trying to save elementtree back into an xml file and it bahaves really strange. I have a function which takes some QTextEdit text arguments (obviously QStrings) and if I save a node property as a QString, it saves the file fine, but if I try to save a node text as a QString it separates each string letter and inserts spaces between them and then makes the file binary instead of text so that I can't even edit it later.

My question is - should all strings be Python strings or they can also be QStrings but I'm doing something wrong? Here's the code extract:

self.tree = ElementTree()
        self.xmlDoc = self.tree.parse(self.xmlFile)
        self.lstProperty = self.xmlDoc.getiterator("property")

...

    def SaveUnits(self, strProperty, strUnit, strNewProperty, strNewUnit):
        for property in self.lstProperty:
            if property.attrib["name"] == strProperty:
                lstUnits = property.getiterator("unit")
                for units in lstUnits:
                    toUnit = units.getchildren()
                    if toUnit[0].text == strUnit:
                        toUnit[0].text = str(strNewUnit)
                property.attrib["name"] = strNewProperty
                self.tree.write(self.xmlFile, "utf-8")
                return True

The xml looks fine:
<property name="Property1">
<unit>
<unitname>unit1</unitname>

If I use 'toUnit[0].text = strNewUnit', then it looks like this:
<property name="Property1">
<unit>
<unitname>u n i t 1 </unitname>

Thank you.

Recommended Answers

All 3 Replies

I do not know other things than that property is one builtin class dealing with decorators.

My question is - should all strings be Python strings

It's always a good idea to convert to Python strings.

I do not know other things than that property is one builtin class dealing with decorators.

Yea, that's right.

class Apple(object):
    def __init__(self):
        self._seeds = 5

    @property
    def seeds(self):
        return self._seeds

myapple = Apple()
print myapple.seeds

myapple.seeds = 4 #raises exception:
#AttributeError: can't set attribute
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.