1

I'm making a game, and I want to use XML to store the data. How do I set variables based off an attribute from the xml tag using ElementTree?

I have already tried assigning a variable objectEx-ssubchild.attrib = ssubchild.text.

import os
import xml.etree.ElementTree as et

##vars

saveEx = "data\saveEx.xml"

base_path = os.path.dirname(os.path.realpath(__file__))

xml_file = os.path.join(base_path, saveEx)


##parsing

treeEx = et.parse(xml_file)

root = treeEx.getroot()

for child in root:
    for subchild in child:
        for ssubchild in subchild:
            print(ssubchild.tag, "-", ssubchild.attrib, ":" , ssubchild.text)
            objectEx-ssubchild.attrib = ssubchild.text ##where the error is
            print(objectEx-charName)

I expected to get a variable called objectEx.charName that holds the string '[name]', however I get an error

can't assign to operator ([unknown], line 23)

I also want to add the fact that I am in no way shape or form 'good' at python. I understand the basics, and the game I am making is text only.

faruk13
  • 1,276
  • 1
  • 16
  • 23
duude
  • 45
  • 4
  • it looks like you're using hyphens in your variable names. that's not allowed - see https://stackoverflow.com/questions/2064329/why-python-does-not-allow-hyphens – Michael D. Feb 16 '19 at 20:52
  • It says that it's an undefined variable, even though I'm assigning to it a value. – duude Feb 18 '19 at 21:33
  • if `objectEx` is an object it needs to be instanciated. `objectEx.charName` and `objectEx_charName` are two different types. First is addressing an objects attribute second a variable. – Michael D. Feb 18 '19 at 23:03

0 Answers0