data-wrangling
data-wrangling copied to clipboard
Problem with XML exercise, Chapter 3
Hello,
I was working through the exercises and had a problem when I tried:
from xml.etree import ElementTree as ET
tree = ET.parse('data-text.xml')
root = tree.getroot()
print list(root)
(pages 57-61). It wouldn't list the elements in the list. I ended up writing a for loop to get it to look like the example in the book (bottom of page 60, top of page 61):
import xml.etree.ElementTree as ET
tree = ET.parse('data-text.xml')
root = tree.getroot()
# data = root.find('Data')
for element in root:
print element
Not sure if I did that correctly or efficiently, but this worked!
Thank you for writing the book.
Edit: Python version:
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Mac OS: Sierra 10.12.3
Thanks!
@AJVicens Thanks for reporting this! Can you let me know what version of Python and what operating system you are using so I can try replicating? I'm glad you found a way -- good experimentation! :+1:
Updated. Sorry about that!
No worries at all! My Macbook is at home, so I will try it there first. What was the response when you simply printed list(tree). Was it an error or just nothing happened? (final question for now, I promise!) :smile:
Sorry for my delay. It was that nothing happened. Given my noob-ness, it could be that I was just doing something wrong. I can try to run through that issue again soon and update the issue.
Hi there,
Following up with this -- it works for me in IPython on 2.7.6. If you have time, can you try running it in IPython and see if you get the same results? You can install with pip or conda install ipython. Let me know how it goes and thanks again for your help! :)
-kj