3 回答
TA贡献2003条经验 获得超2个赞
除了正则表达式,您还可以使用sqlparse并拆分字符串:
import sqlparse
stmts = """PRIMARY INDEX FIRST_ONE ( PLATFORM_CD ,SYSTEM_NAME ,DB_NAME ,
TABLE_NAME ,COLUMN_NAME );
) PRIMARY INDEX SECOND_ONE ( Hq_Id);
PRIMARY INDEX THIRD_ONE ( DB_NAME ,BU_NAME );
PRIMARY INDEX FOURTH_ONE ( DB_NAME ,TABLE_NAME ,COLUMN_NAME, PII_CD);
UNIQUE PRIMARY INDEX FIFT_ONE( DB_NAME,PRIMARY_SYSTEM_NAME);"""
for stmt in sqlparse.split(stmts):
res = sqlparse.parse(stmt) # parses each statement
for tok in (res[0].tokens): # iterate all tokens
if isinstance(tok, sqlparse.sql.Function):
idx, columns = str(tok).split('(')
cols = [c.strip() for c in columns[:columns.find(')')].split(',')]
print (idx.strip(), cols)
返回:
FIRST_ONE ['PLATFORM_CD', 'SYSTEM_NAME', 'DB_NAME', 'TABLE_NAME', 'COLUMN_NAME']
SECOND_ONE ['Hq_Id']
THIRD_ONE ['DB_NAME', 'BU_NAME']
FOURTH_ONE ['DB_NAME', 'TABLE_NAME', 'COLUMN_NAME', 'PII_CD']
FIFT_ONE ['DB_NAME', 'PRIMARY_SYSTEM_NAME']
添加回答
举报