我正在使用python pyparsing libary,其輸出似乎是元組。但是,當試圖訪問作爲元組時,我得到意想不到的結果。Python元組訪問問題?
>>> from pyparsing import *
>>> aaa = (Literal('xxx') + SkipTo(':') + Literal(':')('::') + Word(nums)('eee')).parseString('xxx : 123')
>>> aaa
(['xxx', '', ':', '123'], {'eee': [('123', 3)], '::': [(':', 2)]})
這是什麼怪:
>>> aaa[0]
'xxx'
>>> aaa[1]
''
我希望aaa[0]
是列表:
['xxx', '', ':', '123']
和AAA [1] - 字典:
{'eee': [('123', 3)], '::': [(':', 2)]}
爲什麼我得到意外?這裏發生了什麼?謝謝。
您輸入了aaa嗎? type(aaa) – PyNEwbie
>>> type(aaa) >>> dir(aaa) ['::','__class__','__delattr__','__doc__','__format__ 」, '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__self_class__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__thisclass__', 'EEE'] >>> –
PyNEwbie
看到這個答案對一個類似問題:http://stackoverflow.com/questions/7045757/list-of-dictionaries-and-pyparsing/7047048#7047048可以將ParseResults對象視爲列表,字典或對象。或者你可以使用'asList()'或'asDict()'來轉換它們。但首先嚐試這些訪問:'aaa [0]','aaa ['eee']'和'aaa.eee'。我有些迷惑,你有一些沒有出現在列表中的命名結果中的值,以及爲什麼有一個名爲「::」的命名元素。但是這些都與這個問題相關。如果你打印出'aaa.dump()',你會看到更好的細節。 – PaulMcG