I am trying to parse some SQL statements (CREATE TABLE exactly) using pyparsing. For both database name and table table I have created identifiers:
identifier = (Combine(Optional('"') + Word(alphanums) +
ZeroOrMore('_' + Word(alphanums)) +
Optional('"')) & ~keywords_set)
database_name = identifier.setResultsName('database_name')
table_name = identifier.setResultsName('table_name')
I am also using this parsing method:
def parse(self, sql):
try:
tokens = self.create_table_stmt.parseString(sql)
print tokens.database_name, tokens.table_name
values = tokens.database_name, tokens.table_name
print values
return values
except ParseException as error:
print error
For following input:
CreateTableParser().parse('''
CREATE TABLE "django"."django_site1" (
)''')
i get:
['"django"'] ['"django_site1"']
((['"django"'], {}), (['"django_site1"'], {}))
Why these are different? How can I just get the output in the first way, as simple lists? I only get it when I print those values.
There is a difference between
print a, bandprint (a,b):print a, bprints two objectsaandb.print (a, b)prints a single object the tuplea, b:Or to put it another way:
__str__method is called when you dostr(obj). If there is no__str__method then__repr__method is calledrepr(obj).