Forum Archive

For loop Action_Out?

TutorialDoctor

When going through a list in a for loop, inside of a python script, I can print the whole list in the console, but I cannot use the workflow.set_output() function to pass the entire list to the next action.

It only passes the last item.

For example:

tags = linguistictagger.tag_string(text, linguistictagger.SCHEME_LEXICAL_CLASS)
for tag in tags:
    lex = tag[0]
    word = tag[1]
    out = word +  ' = ' + lex
    print(out)

This works, but

tags = linguistictagger.tag_string(text, linguistictagger.SCHEME_LEXICAL_CLASS)
for tag in tags:
    lex = tag[0]
    word = tag[1]
    out = word +  ' = ' + lex
    workflow.set_output(out)

This doesn't work.

omz

The output is only passed to the next action after the script has finished. But you can of course build a string of multiple lines within the script, and then set the entire output after your loop has finished.

tags = linguistictagger.tag_string(text, linguistictagger.SCHEME_LEXICAL_CLASS)
lines_out = []
for tag in tags:
    lex = tag[0]
    word = tag[1]
    out = word +  ' = ' + lex
    lines_out.append(out)
workflow.set_output('\n'.join(lines_out))
ccc

Or...

tags = linguistictagger.tag_string(text, linguistictagger.SCHEME_LEXICAL_CLASS)
workflow.set_output('\n'.join(['{1} = {0}'.format(*tag) for tag in tags]))
TutorialDoctor

Thank you so much! I have now finished my "Parts of Speech" workflow!

If anyone wants to check it out:

Parts of Speech