编写正则表达式时,可以跨多行编写表达式并包含注释,然后在传递编译版本之前使用re.VERBOSE
选项编译表达式。我想用pandas.read_sql_query
做类似的事情。
例如,而不是:
result = pd.read_sql_query('select a.gvkey, a.tic, a.datadate as fyearend, year(a.datadate) as year, month(a.datadate) as fyrc, b.datadate, month(b.datadate) as month, b.trt1m from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate) where a.TIC = "IBM" and a.datafmt = "STD" and a.consol="C" and a.indfmt = "INDL" and year(a.datadate)>1980', engine)
我想写一些像:
q = """select a.gvkey,
a.tic, #COMMENTS
a.datadate as fyearend, #COMMENTS
year(a.datadate) as year, #COMMENTS
month(a.datadate) as fyrc, b.datadate,
month(b.datadate) as month,
b.trt1m
from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate)
where a.TIC = "IBM"
and a.datafmt = "STD"
and a.consol="C"
and a.indfmt = "INDL"
and year(a.datadate)>1980
"""
result = p.read_sql_query(q ,engine)
我的问题与this有关跨多行拆分python命令的问题有关,但我想在查询中添加注释。
正如我所提到的,我在pandas / SQL案例中想要做的事情类似于使用re.VERBOSE
的正则表达式案例中可以做的事情。以下是regex的示例:
pattern = r'''\s(shares?| #COMMENTS
warrants?| #COMMENTS
stock| #AND SO ON...
(non)?vest(ed)?
)\b
'''
crit = re.compile(pattern_nopt, re.VERBOSE)
match=re.search(crit, string)
这将使查询更具可读性,我发现在与共同作者共享代码时,详尽地对查询进行注释很重要。
答案 0 :(得分:5)
是的它会起作用,但你必须使用正确的comment delimiter for SQLite:
--
代表内联评论
多行注释的/* foo.. */
(如C)
所以它看起来像:
q = """select a.gvkey,
a.tic, -- COMMENTS
a.datadate as fyearend, -- COMMENTS
year(a.datadate) as year, /* Another very long
and multi-lines comment... */
month(a.datadate) as fyrc, b.datadate,
month(b.datadate) as month,
b.trt1m from COMPM.FUNDA a join COMPM.SECM b on a.gvkey = b.gvkey and year(a.datadate) = year(b.datadate)
where a.TIC = "IBM"
and a.datafmt = "STD"
and a.consol="C"
and a.indfmt = "INDL"
and year(a.datadate)>1980
"""
result = p.read_sql_query(q, conn)