2013-04-15 4 views
0

У меня есть данные xml.разбор XML в Python

<rentals type="array"> 
    <rental> 
    <account-id type="integer">130</account-id> 
    <created-at type="datetime">2011-08-26T23:27:13Z</created-at> 
    <description>1 - 1 br apt, walk-up - </description> 
    <id type="integer">218</id> 
    <name>Apt.1</name> 
    <notes nil="true"/> 
    <position type="integer">1</position> 
    <updated-at type="datetime">2011-12-06T09:00:58Z</updated-at> 
    </rental> 
    <rental> 
    <account-id type="integer">130</account-id> 
    <created-at type="datetime">2011-08-22T03:06:49Z</created-at> 
    <description>2 - Alcove Studio, walk-up, remote 3</description> 
    <id type="integer">210</id> 
    <name>Apt.2</name> 
    <notes nil="true"/> 
    <position type="integer">2</position> 
    <updated-at type="datetime">2012-09-26T21:37:20Z</updated-at> 
    </rental> 

</rentals> 

Я использовал библиотеку Pyxml2obj питона, чтобы разобрать это в питона Словарь .Using XMLin

заходит ответ, как.

{u'rental': {u'Apt.19': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'19 - big, spot lights studio', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-11T18:03:24Z', u'type': u'datetime'}, u'created-at': {'content': u'2013-01-19T02:07:02Z', u'type': u'datetime'}, u'position': {'content': u'11', u'type': u'integer'}, u'id': {'content': u'1303', u'type': u'integer'}}, u'Apt.20': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'20 - same as 19', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-14T23:19:58Z', u'type': u'datetime'}, u'created-at': {'content': u'2013-01-19T02:07:23Z', u'type': u'datetime'}, u'position': {'content': u'12', u'type': u'integer'}, u'id': {'content': u'1304', u'type': u'integer'}}, u'Apt.8': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'8 - studio, walk-up', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-08T13:18:07Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-08-27T01:25:36Z', u'type': u'datetime'}, u'position': {'content': u'5', u'type': u'integer'}, u'id': {'content': u'222', u'type': u'integer'}}, u'Apt.10': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'10 - semi luxury studio, elevator', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-08T13:40:52Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-09-01T15:02:21Z', u'type': u'datetime'}, u'position': {'content': u'6', u'type': u'integer'}, u'id': {'content': u'226', u'type': u'integer'}}, u'Apt.7': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'7 - studio, remote', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-07T20:40:58Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-08-27T01:25:00Z', u'type': u'datetime'}, u'position': {'content': u'4', u'type': u'integer'}, u'id': {'content': u'221', u'type': u'integer'}}, u'Apt.5': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'5 - huge studio', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-07T19:24:30Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-08-27T01:23:56Z', u'type': u'datetime'}, u'position': {'content': u'3', u'type': u'integer'}, u'id': {'content': u'219', u'type': u'integer'}}, u'Apt.2': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'2 - Alcove Studio, walk-up, remote 3', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2012-09-26T21:37:20Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-08-22T03:06:49Z', u'type': u'datetime'}, u'position': {'content': u'2', u'type': u'integer'}, u'id': {'content': u'210', u'type': u'integer'}}, u'Apt.15': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'15 - alcove, walk-up', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-14T22:39:41Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-11-04T20:13:49Z', u'type': u'datetime'}, u'position': {'content': u'9', u'type': u'integer'}, u'id': {'content': u'344', u'type': u'integer'}}, u'Apt.16': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'16 - Studio', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-09T23:12:45Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-11-29T06:12:33Z', u'type': u'datetime'}, u'position': {'content': u'10', u'type': u'integer'}, u'id': {'content': u'378', u'type': u'integer'}}, u'Apt.1': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'1 - 1 br apt, walk-up - ', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2011-12-06T09:00:58Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-08-26T23:27:13Z', u'type': u'datetime'}, u'position': {'content': u'1', u'type': u'integer'}, u'id': {'content': u'218', u'type': u'integer'}}, u'Apt.25': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'studio with back yard', u'notes': {}, u'updated-at': {'content': u'2013-04-14T20:32:07Z', u'type': u'datetime'}, u'created-at': {'content': u'2013-04-01T13:02:59Z', u'type': u'datetime'}, u'position': {'content': u'17', u'type': u'integer'}, u'id': {'content': u'2683', u'type': u'integer'}}, u'Apt.24': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'24 - studio', u'notes': {}, u'updated-at': {'content': u'2013-04-12T16:18:23Z', u'type': u'datetime'}, u'created-at': {'content': u'2013-03-22T21:15:29Z', u'type': u'datetime'}, u'position': {'content': u'16', u'type': u'integer'}, u'id': {'content': u'1910', u'type': u'integer'}}, u'Apt.23': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'23 - alcove studio', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-15T01:01:13Z', u'type': u'datetime'}, u'created-at': {'content': u'2013-02-02T19:06:33Z', u'type': u'datetime'}, u'position': {'content': u'15', u'type': u'integer'}, u'id': {'content': u'1351', u'type': u'integer'}}, u'Apt.11': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'11 - Split Studio, remote', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-11T20:02:19Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-10-07T14:55:38Z', u'type': u'datetime'}, u'position': {'content': u'7', u'type': u'integer'}, u'id': {'content': u'308', u'type': u'integer'}}, u'Apt.21': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'21 - walk up, alcove studio', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-14T06:05:49Z', u'type': u'datetime'}, u'created-at': {'content': u'2013-02-02T19:05:03Z', u'type': u'datetime'}, u'position': {'content': u'13', u'type': u'integer'}, u'id': {'content': u'1349', u'type': u'integer'}}, u'Apt.22': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'22 - 1br big', u'notes': {u'nil': u'true'}, u'updated-at': {'content': u'2013-04-10T13:45:47Z', u'type': u'datetime'}, u'created-at': {'content': u'2013-02-02T19:06:08Z', u'type': u'datetime'}, u'position': {'content': u'14', u'type': u'integer'}, u'id': {'content': u'1350', u'type': u'integer'}}, u'Apt.14': {u'account-id': {'content': u'130', u'type': u'integer'}, u'description': u'14 - 1 BR walk-up', u'notes': {}, u'updated-at': {'content': u'2013-04-12T17:31:31Z', u'type': u'datetime'}, u'created-at': {'content': u'2011-11-01T03:37:15Z', u'type': u'datetime'}, u'position': {'content': u'8', u'type': u'integer'}, u'id': {'content': u'339', u'type': u'integer'}}}, u'type': u'array'} 

Теперь я пытаюсь перебрать эту Dict, чтобы получить индивидуальные данные, такие как,

for rentall in getdictobj.items(): 
    rentall.name 

Я получаю ошибку

AttributeError: 'tuple' object has no attribute 'name' 

Я знаю, что объект не имеет имени атрибутов но в названии Xml четко указано.

Пожалуйста, помогите мне понять, что, возможно, я делаю неправильно здесь

+0

вы пропустили colsing тегов для последнего проката – user1451340

+0

что одна моя ошибка записи, так как XML является слишком большим – burning

+0

Как вы получаете ключи Apt.19, содержание в файле ответов? Эти ключи недоступны как узлы в XML. Можете ли вы попробовать с помощью анализатора xml.dom.minidom, вам будет легко разобрать и получить все узлы имен. – kvivek

ответ

0

Dict.items возвращает список кортежей (имя. Value), где имя является индекс ДИКТ, и может быть все, что не является изменяемым объект. Вы должны сказать, имя, значение в .....

Пример:

for i, value in d.items(): 
    print i, value 

Или, вы можете преобразовать Dict для кортежа или списка так:

T = tuple (d.items()) 
T = list (d.items()) 

и теперь вы можете может выполнять итерацию по кортежу/списку вместо dict.

+0

, пожалуйста, уточните еще – burning

+0

'd = {'a': 1, 'b': 2, 'c ': 3} 'then' для k, v в d.items()' будет иметь буквы (ключи) в 'k' и числа (значения) в' v' на каждой итерации. –

0

Существует альтернативный способ получить все узлы имен.

from xml.dom import minidom 
xmldoc = minidom.parse(r'C:\Users\kvivek\Desktop\rentals.xml') 
nameNodes = xmldoc.getElementsByTagName('name') 
for nameNode in nameNodes: 
    print namenode.toxml() 
Смежные вопросы