我正在尝试为某些命令编写sql查询解析器,以使它们以列表形式显示在查询中。但是我正在使用PyParsing的runTests(),它提供了布尔值和一行输出。 如何从列表行中删除所有空字典和元组。
我曾尝试直接为单个查询打印表名或列,但不能为该查询打印。请帮助我将输出打印在列表上,以便以后可以在csv文件中获取。
这只是代码的一小部分。我从PyParsing github存储库中引用了sqlparsing。
from pyparsing import *
ParserElement.enablePackrat()
delete_stmt = Forward().setName("delete statement")
LPAR,RPAR,COMMA = map(Suppress,"(),")
del_source = ((Group(database_name("database") + "." + table_name("table*")) | table_name("table*")) +
Optional(Optional(AS) + table_alias("table_alias*")) +
Optional(INDEXED + BY + index_name("name") | NOT + INDEXED)("index"))
delete_core = (DELETE + Optional(FROM + del_source("from*")) + Optional(WHERE + expr("where_expr")))
delete_stmt << (delete_core)
test3="""\
delete from abc where c=book
"""
delete_list=select_stmt.runTests(test3)
print(delete_list)
这就是我要得到的:我需要上半部分,但列表会打印从布尔部分开始的最后一个部分,即select_list。
['DELETE', 'FROM', 'abc', 'WHERE', ['c', '=', 'book']]
delete from abc where c=book
['DELETE', 'FROM', 'abc', 'WHERE', ['c', '=', 'book']]
-from: [['abc']]
[0]:
['abc']
-table: [['abc']]
[0]:
['abc']
-where_expr: ['c', '=', 'book']
(True, [('delete from abc where c=book', (['DELETE', 'FROM', 'abc',
'WHERE',
(['c', '=', 'book'], {})], {'table': [(['abc'], {})], 'from': [(['abc'],
{})], 'where_expr': [(['c', '=', 'book'], {})]}))])