I am new to the pyparsing module and find it such a nice tool for parsing. However, when I try to test the resulting lists with the isinstance function for a list, it doesn't seem to work for me when using Python 3.7.
The following 2 programs with their output hopefully encapsulate the issue that I am having.
Program with pyparsing...
import pyparsing as pp
if __name__ == '__main__':
print("Testing isinstance with pyparsing...")
greet = pp.Word(pp.alphas) + "," + pp.Word(pp.alphas) + "!"
for greeting_str in ["Hello, World!","Bonjour, Monde!","Hola, Mundo!","Hallo, Welt!",]:
greeting = greet.parseString(greeting_str)
print("Parsed greeting: ", greeting)
res = isinstance(greeting, list)
print("Is this a list? -> ", res)
with resulting output...
Testing isinstance with pyparsing...
Parsed greeting: ['Hello', ',', 'World', '!']
Is this a list? -> False
Parsed greeting: ['Bonjour', ',', 'Monde', '!']
Is this a list? -> False
Parsed greeting: ['Hola', ',', 'Mundo', '!']
Is this a list? -> False
Parsed greeting: ['Hallo', ',', 'Welt', '!']
Is this a list? -> False
Program without pyparsing import...
if __name__ == '__main__':
print("Testing isinstance without pyparsing...")
parsed_greetings = [['Hello', ',', 'World', '!'], ['Bonjour', ',', 'Monde', '!'], ['Hola', ',', 'Mundo', '!'], ['Hallo', ',', 'Welt', '!']]
for parsed_greeting in parsed_greetings:
print("Parsed greeting: ", parsed_greeting)
res = isinstance(parsed_greeting, list)
print("Is this a list? -> ", res)
with the resulting output...
Testing isinstance without pyparsing...
Parsed greeting: ['Hello', ',', 'World', '!']
Is this a list? -> True
Parsed greeting: ['Bonjour', ',', 'Monde', '!']
Is this a list? -> True
Parsed greeting: ['Hola', ',', 'Mundo', '!']
Is this a list? -> True
Parsed greeting: ['Hallo', ',', 'Welt', '!']
Is this a list? -> True
In summary, the isinstance function for testing a list seems to be working as expected when not using pyparsing, but when I use pyparsing it doesn't seem to work correctly. I looked through the pyparsing documentation but could not find any mention of this issue or a possibility of using list as a variable name.
Using the isinstance function for testing a list is very helpful in some of my later processing, so any insight into this is appreciated.