0

If I have an xml attribute that is declared like:

for child in  root:
  print(child.attrib)

and it prints out all of my child nodes:

{'action': 'Stay', 'response': ''}
{'action': 'Enemy Charging User', 'response': ''}
{'action': 'Move', 'response': ''}

is there a way I can just convert the actions to a string list where I can use the elements from it? for example 'Stay', 'Enemy Charging User', and 'Move' would convert to strings and be stored into a string list called l1[] where I can say call l1[0] and print out 'Stay'?

1 Answer 1

1
l1 = [ child.attrib['action'] for child in root ]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This is what I was looking. But just to expand on it, say if I were to use different xml files with different attribute names, is there a way I can do this without changing the ['action'] attribute to match the xml file every time?
You'd have to figure out what the appropriate replacement was. Certainly you could replace 'action' with a variable in the expression above.

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.