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, b and print (a,b):
>>> a, b = "ab"
>>> a
'a'
>>> b
'b'
>>> print a, b
a b
>>> print (a, b)
('a', 'b')
print a, b prints two objects a and b. print (a, b) prints a single object the tuple a, b:
>>> w = sys.stdout.write
>>> _ = w(str(a)), w(' '), w(str(b)), w('\n')
a b
>>> _ = w(str((a,b))), w('\n')
('a', 'b')
Or to put it another way:
>>> class A:
... def __str__(self):
... return '1'
... def __repr__(self):
... return 'A()'
...
>>> print A(), A()
1 1
>>> print (A(), A())
(A(), A())
__str__ method is called when you do str(obj). If there is no __str__ method then __repr__ method is called repr(obj).
加载中,请稍侯......
精彩评论