[英]Regex for String Parse
I am attempting to build a list of column values and declaration types in Python based on this String, which is an SQL creation query: 我试图基于此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);'''
I have already parsed the quoted values out of the expression like so: 我已经从表达式中解析出引用的值,如下所示:
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)
How do I go about parsing the SQL type values from each line? 我该如何解析每一行的SQL类型值?
For example, I have already gathered id
and clientID
in a list. 例如,我已经在列表中收集了
id
和clientID
。 Now I want the declaration, serial NOT NULL PRIMARY KEY
and integer NOT NULL
, etc... in a list. 现在,我要在列表中声明,
serial NOT NULL PRIMARY KEY
和integer NOT NULL
等。
How can I accomplish this type of parsing? 如何完成这种类型的解析? I dict would be best but I can figure that out on my own, I just need the regex
我决定是最好的,但我可以自己弄清楚,我只需要正则表达式
Update 更新
Here is updated code that provides me a dictionary of everything I need based on the creation_sql statement: 这是更新的代码,该代码根据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)
Try this: 尝试这个:
import re
r = re.findall('"(.*?)"\s+(.+)?[,)]', creation_sql)
print(dict(r))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.