Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
how to import xml documentos using pyhton to read the data from it?
i have a xml document that is an output of a nmap scan, now I'm trying to import into python to be able to read the file by different elements for example:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
if im using this file and i just want to print the countries i can do something like:
>>> for country in countr:
... print country.get('name')
...
Liechtenstein
Singapore
Panama
____________________________________________
I'm able to do most of the thing with the element tree, like find, finfall, etc, my main problem is how to import my file into python to be able to read the data
the file is in: C:/Users/Heleno/Desktop/results/primeiro.xml
im trying to do the tutorial:
http://docs.python.org/2/library/xml.etree.element...
but still dont understand how they manage to import the file.
the bit of code i have:
import xml.etree.ElementTree as ET
tree = ET.parse('primeiro.xml')
root = tree.getroot()
i put this into python idle but i think i need to be more specific with the root where the file is
1 Answer
- ZakLv 58 years agoFavorite Answer
I think your assessment is correct. If there is an error being generated, I'd recommend including the text of the error message next time. But, unless you're running your python script from the same directory as your xml file, then python won't be able to find your xml file.
The line of code that reads: tree = ET.parse('primeiro.xml') is the culprit.
What you'll want to use instead of 'primerio.xml' is the full path to the file, namely 'C:/Users/Heleno/Desktop/results/primeiro.xml'