我有一份姓名和年龄的档案,
john 25
bob 30
john bob 35
这是我到目前为止所拥有的
from pyparsing import *
data = '''
john 25
bob 30
john bob 35
'''
name = Word(alphas + Optional(' ') + alphas)
rowData = Group(name +
Suppress(White(" ")) +
Word(nums))
table = ZeroOrMore(rowData)
print table.parseString(data)
我期待的输出是
[['john', 25], ['bob', 30], ['john bob', 35]]
这是堆栈跟踪
Traceback (most recent call last):
File "C:\Users\mccauley\Desktop\client.py", line 11, in <module>
eventType = Word(alphas + Optional(' ') + alphas)
File "C:\Python27\lib\site-packages\pyparsing.py", line 1657, in __init__
self.name = _ustr(self)
File "C:\Python27\lib\site-packages\pyparsing.py", line 122, in _ustr
return str(obj)
File "C:\Python27\lib\site-packages\pyparsing.py", line 1743, in __str__
self.strRepr = "W:(%s)" % charsAsStr(self.initCharsOrig)
File "C:\Python27\lib\site-packages\pyparsing.py", line 1735, in charsAsStr
if len(s)>4:
TypeError: object of type 'And' has no len()