-2

I am very new to python and using pyparsing but getting some exception with following code

while site_contents.find('---', line_end) != line_end + 2:
        cut_start = site_contents.find(" ", site_contents.find("\r\n", start)) 
        cut_end = site_contents.find("  ", cut_start+1) 
        line_end = site_contents.find("\r\n", cut_end)
        name = site_contents[cut_start:cut_end].strip() 
        float_num = Word(nums + '.').setParseAction(lambda t:float(t[0]))
        nonempty_line = Literal(name) + Word(nums+',') + float_num + Suppress(Literal('-')) + float_num * 2 
        empty_line = Literal(name) + Literal('-') 
        line = nonempty_line | empty_line
        parsed = line.parseString(site_contents[cut_start:line_end]) 
        start = line_end 

Exception

Traceback (most recent call last):
      File "D:\Ecllipse_Python\HellloWorld\src\HelloPython.py", line 108, in <module>
        parsed = line.parseString(site_contents[cut_start:line_end]) # parse line of data following cut name
      File "C:\Users\arbatra\AppData\Local\Continuum\Anaconda\lib\site-packages\pyparsing.py", line 1041, in parseString
        raise exc
    pyparsing.ParseException: Expected W:(0123...) (at char 38), (line:1, col:39)

how to resolve this issue?

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
Rahul
  • 11
  • 3
  • Can you post a small sample of some of the input text you are trying to parse? The error you are getting is failing to match one of the numeric fields, that it is not being found where you expect it. – PaulMcG Jan 19 '15 at 08:21

1 Answers1

0

You'll get a little better exception message if you give names to your expressions, using setName. From the "Expected W:(0123...)" part of the exception message, it looks like the parser is not finding a numeric value where it is expected. But the default name is not showing us enough to know which type of numeric field is expected. Modify your parser to add setName as shown below, and also change the defintion of nonempty_line:

float_num = Word(nums + '.').setParseAction(lambda t:float(t[0])).setName("float_num")
integer_with_commas = Word(nums + ',').setName("int_with_commas")
nonempty_line = Literal(name) + integer_with_commas + float_num + Suppress(Literal('-')) + float_num * 2 

I would also preface the call to parseString with:

print site_contents[cut_start:line_end]

at least while you are debugging. Then you can compare the string being parsed with the error message, including the column number where the parse error is occurring, as given in your posted example as "(at char 38), (line:1, col:39)". "char xx" starts with the first character as "char 0"; "col:xx" starts with the first column as "col:1".

These code changes might help you pinpoint your problem:

print "12345678901234567890123456789012345678901234567890"
print site_contents[cut_start:line_end]
try:
    parsed = line.parseString(site_contents[cut_start:line_end])
except ParseException as pe:
    print pe.loc*'  ' + '^'
    print pe

Be sure to run this in a window that uses a monospaced font (so that all the character columns line up, and all characters are the same width as each other).

Once you've done this, you may have enough information to fix the problem yourself, or you'll have some better output to edit into your original question so we can help you better.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130