Parsing names with pyparsing

Posted by johnthexiii on Stack Overflow See other posts from Stack Overflow or by johnthexiii
Published on 2012-10-25T16:39:13Z Indexed on 2012/10/25 17:00 UTC
Read the original article Hit count: 257

Filed under:
|

I have a file of names and ages,

john 25 
bob 30 
john bob 35

Here is what I have so far

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)

the output I am expecting is

[['john', 25], ['bob', 30], ['john bob', 35]]

Here is the stacktrace

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()

© Stack Overflow or respective owner

Related posts about python

Related posts about pyparsing