2

The following code gives me the error 'no such attribute _ParseResuls__tokdict' when run on an input with more than one line.

With single-line files, there is no error. If I comment out either the second or third line shown here, then I don't get that error either, no matter how long the file is.

for line in input:
    final = delimitedList(expr).parseString(line)
    notid = delimitedList(notid).parseString(line)
    dash_tags = ', '.join(format_tree(notid))

    print final.lineId + ": " + dash_tags

Does anyone know what's going on here?

EDIT: As suggested, I'm adding the complete code to allow others to reproduce the error.

from pyparsing import *

#first are the basic elements of the expression

#number at the beginning of the line, unique for each line
#top-level category for a sentiment
#semicolon should eventually become a line break

lineId = Word(nums)
topicString = Word(alphanums+'-'+' '+"'")
semicolon = Literal(';')

#call variable early to allow for recursion
#recursive function allowing for a line id at first, then the topic,
#then any subtopics, and so on. Finally, optional semicolon and repeat.
#set results name lineId.lineId here
expr = Forward()
expr << Optional(lineId.setResultsName("lineId")) + topicString.setResultsName("topicString") + \
Optional(nestedExpr(content=delimitedList(expr))).setResultsName("parenthetical") + \
Optional(Suppress(semicolon).setResultsName("semicolon") + expr.setResultsName("subsequentlines"))

notid = Suppress(lineId) + topicString + \
Optional(nestedExpr(content=delimitedList(expr))) + \
Optional(Suppress(semicolon) + expr)



#naming the parenthetical portion for independent reference later
parenthetical = nestedExpr(content=delimitedList(expr))


#open files for read and write
input = open('parserinput.txt')
output = open('parseroutput.txt', 'w')

#defining functions

#takes nested list output of parser grammer and translates it into
#strings suited for the final output
def format_tree(tree):                                                                                            
    prefix = ''
    for node in tree:
        if isinstance(node, basestring):
            prefix = node
            yield node
        else:
            for elt in format_tree(node):
                yield prefix + '_' + elt

#function for passing tokens from setResultsName
def id_number(tokens):
    #print tokens.dump()
    lineId = tokens
    lineId["lineId"] = lineId.lineId

def topic_string(tokens):
    topicString = tokens
    topicString["topicString"] = topicString.topicString

def parenthetical_fun(tokens):
    parenthetical = tokens
    parenthetical["parenthetical"] = parenthetical.parenthetical

#function for splitting line at semicolon and appending numberId
#not currently in use
def split_and_prepend(tokens):
    return '\n' + final.lineId


#setting parse actions
lineId.setParseAction(id_number)
topicString.setParseAction(topic_string)
parenthetical.setParseAction(parenthetical)


#reads each line in the input file
#calls the grammar expressed in 'expr' and uses it to read the line and assign names to the tokens for later use
#calls the 'notid' varient to easily return the other elements in the line aside from the lineId
#applies the format tree function and joins the tokens in a comma-separated string
#prints the lineId + the tokens from that line
for line in input:
    final = delimitedList(expr).parseString(line)
    notid = delimitedList(notid).parseString(line)
    dash_tags = ', '.join(format_tree(notid))

    print final.lineId + ": " + dash_tags

The input file is a txt document with the following two lines:

1768    dummy; data
1768    dummy data; price
6
  • 1
    Post examples that demonstrate the problem with exact error messages. This won't run as is to reproduce the error. What was the input? What was the stack trace in the error message? What are expr and notid? Commented Apr 28, 2012 at 23:53
  • Thanks, Mark. I'm new to programming and to StackOverflow, so I appreciate the pointers. The stack trace in the error message was quite lengthy, and I don't know how to copy/paste from the console where I'm running the script. Is that good enough or should I type out the error message, etc.?
    – Charles W
    Commented Apr 29, 2012 at 3:08
  • It's better to try to reduce the code to the smallest amount that can reproduce the issue, but I think my answer below is the cause. notid's value is being changed in the loop, destroying its pay parsing expression. Commented Apr 29, 2012 at 7:42
  • Any suggestions on how one might fix such a loop?
    – Charles W
    Commented Apr 29, 2012 at 13:50
  • Ah, sorry - I didn't notice the last sentence of the response somehow. :) I'll try what you've suggested.
    – Charles W
    Commented Apr 29, 2012 at 21:15

1 Answer 1

2

Reassigning of notid breaks the second iteration when used in delimitedList. Your third line destroys the notid expression defined earlier in the code, so it will only work the first iteration. Use a different name for the notid assignment.

1
  • You seem to have been right. The problem was that I was (sloppily and unintentionally) using the same variable name for two different things. Thanks for being patient with me!
    – Charles W
    Commented Apr 29, 2012 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.