2012-12-04 6 views
5

के साथ केएमएल बैचजीओ फ़ाइल से समन्वय निकालें मैंने बैचजीओ को कुछ पते अपलोड किए हैं और परिणामस्वरूप केएमएल फ़ाइल डाउनलोड की है जिससे मैं निर्देशांक निकालना चाहता हूं। मैं jumbled टेक्स्ट फ़ाइल को ऑनलाइन here को प्रसन्न करने में कामयाब रहा, लेकिन मुझे नहीं पता कि समन्वय निकालने के लिए इसे कैसे पार्स किया जाए।पाइथन

<?xml version="1.0" ?> 
<kml xmlns="http://earth.google.com/kml/2.0"> 
    <Document> 
     <Placemark> 
      <name>...</name> 
      <description>....</description> 
      <Point> 
       <coordinates>-3.1034345755337,57.144817425039,0</coordinates> 
      </Point><address>...</address> 
      <styleUrl>#0</styleUrl> 
     </Placemark> 
    </Document> 
</kml> 

वहाँ अजगर के लिए कई kml पुस्तकालयों लेकिन प्रलेखन (जैसे pyKML) के रास्ते में ज्यादा नहीं होने लगते हैं। ट्यूटोरियल का उपयोग करना, मैं इतनी दूर मिल गया है और एक 'lxml.etree._ElementTree' ऑब्जेक्ट बनाया है, लेकिन मैं उसके गुण के बारे में सुनिश्चित नहीं कर रहा हूँ:

AttributeError: 'lxml.etree._ElementTree' object has no attribute 'Element' 

तो:

from pykml import parser 

kml_file = "BatchGeo.kml" 

with open(kml_file) as f: 

    doc = parser.parse(f) 

coordinate = doc.Element("coordinates") 
print coordinate 

यह त्रुटि देता है मैं समन्वय की सूची कैसे प्राप्त करूं? धन्यवाद।

उत्तर

10
from pykml import parser 

root = parser.fromstring(open('BatchGeo.kml', 'r').read()) 
print root.Document.Placemark.Point.coordinates 

the pykml docs

आशा व्यक्त की कि मदद करता है देखो!