죄송합니다.이 문제에 관해 많은 스레드가 있지만이 질문을 보내주십시오. 그러나 그들 중 누구도 지금 내 필요를 채우고 있지 않습니다.python xml 처리 - 모든 요소를 반복하는 방법?
나는 XML 파일이 있습니다.
<RestaurantList>
<Restaurant id="100000">
<Url>http://www.example.com/</Url>
<Name>TestRestaurant</Name>
<Description>
<Text>This restaurant has a generous selection of fine wines</Text>
</Description>
</Restaurant>
<Restaurant id="100001">
<Url>http://www.example.com/</Url>
<Name>TestRestaurant1</Name>
<Description>
<Text>This restaurant1 has a generous selection of fine wines</Text>
</Description>
</Restaurant>
</RestaurantList>
모든 요소를 반복하고 일부를 가져 와서 db에 저장하고 싶습니다. Python에서 lxml
또는 xml
과 같은 라이브러리를 보았습니다. 그리고이 시도 :
import xml.etree.ElementTree as ET
file = ET.parse(settings.MEDIA_ROOT+'\\table.xml')
rests = file.xpath('//Restaurant')
for each in rests:
self.stdout.write(each)
하지만 ElementTree has no object attribute xpath
오류가 발생하고,
이 일을하는 가장 좋은 방법은 무엇입니까
? 미리 감사드립니다.
감사합니다 날 – doniyor
미안, 난 다른 오류 메시지가 표시되었다 확인할 수 있습니다. 다시 한번들시겠습니까? – doniyor
@doniyor 오류 메시지는 ElementTree가'xpath'를 지원하지 않는다는 것을 알려줍니다. 내가 말했듯이, 나는 lxml로 코드를 테스트했다. (보다 정확하게'import lxml.etree as ET') 완벽하게 동작했다. – user4815162342