我正在尝试基于此String(它是一个SQL创建查询)在Python中构建列值和声明类型的列表:
creation_sql = '''CREATE TABLE "objects_users" (
"id" serial NOT NULL PRIMARY KEY,
"clientID" integer NOT NULL,
"email" varchar(100) NOT NULL,
"first_name" varchar(100) NOT NULL,
"last_name" varchar(100) NOT NULL,
"phone" varchar(100) NOT NULL,
"password" varchar(100) NOT NULL,
"type" varchar(100) NOT NULL,
"date_created" timestamp with time zone NOT NULL,
"active" varchar(100) NOT NULL);'''
我已经像这样解析出表达式中的引用值:
def parse(creation_query):
quoted_values = re.compile('"[^"]*"')
values = quoted_values.findall(creation_sql)
column_names = []
for value in values:
column_names.append(value)
table_name = column_names[0]
column_names = [names.replace('"','') for names in column_names]
del column_names[0] # this will be table name
print(table_name)
print(column_names)
如何解析每一行的SQL类型值?
例如,我已经在列表中收集了id
和clientID
。现在,我要在列表中声明serial NOT NULL PRIMARY KEY
和integer NOT NULL
等。
如何完成这种类型的解析?我决定是最好的,但我可以自己弄清楚,我只需要正则表达式
更新
以下是更新的代码,该代码根据creation_sql语句为我提供了我需要的所有内容的字典:
import re
creation_sql = '''CREATE TABLE "objects_users" (
"id" serial NOT NULL PRIMARY KEY,
"clientID" integer NOT NULL,
"email" varchar(100) NOT NULL,
"first_name" varchar(100) NOT NULL,
"last_name" varchar(100) NOT NULL,
"phone" varchar(100) NOT NULL,
"password" varchar(100) NOT NULL,
"type" varchar(100) NOT NULL,
"date_created" timestamp with time zone NOT NULL,
"active" varchar(100) NOT NULL);'''
def parse(creation_query):
quoted_values = re.compile('"[^"]*"')
table_name = quoted_values.findall(creation_query)[0]
dictionary_values = dict(re.findall('"(.*?)"\s+(.+)[,)]', creation_query))
dictionary_values['table_name'] = table_name.replace('"','')
return dictionary_values
d = parse(creation_sql)
print(d.get('table_name'))
for key, item in d.items():
if key != 'table_name':
print(key + ":"+ item)
答案 0 :(得分:1)
尝试一下:
import re
r = re.findall('"(.*?)"\s+(.+)?[,)]', creation_sql)
print(dict(r))