Larry Fitzgerald Larry Fitzgerald - 6 months ago 30
Python Question

print attribute of element as string from XML file into a list

Im trying to parse data from an XML file. YES, i have read the official documentation, and looked at many examples. I am stuck.

The main problem is to return the data from the XML file into a string, what i'm getting now is individual characters being returned. (see output)

# A function to return these specific values to a list:
bookName = []
def return_book_name():
for child in root[1][1].text:
bookName.append(child)
return (str (bookName))





output - ['M', 'i', 'd', 'n', 'i', 'g', 'h', 't', ' ', 'R', 'a', 'i', 'n']

as you can see, I tried to return the list "bookname" as a string, but it made no difference. I also tried using STR for the child element, with the same results. I cannot get the output into the list to be a string...

Thank you for your help!

Answer Source

Strings are iterable in Python, that is why you are getting individual characters. Just return the text node directly:

# A function to return these specific values to a list:
bookName = []
def return_book_name():
    return root[1][1].text
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download