[英]Snowflake's REGEXP_SUBSTR with Python connector
I am building a query with regex using the web UI, something like this:我正在使用 web UI 使用正则表达式构建查询,如下所示:
SELECT uid, REGEXP_SUBSTR(PAGEPAGEPATHLEVEL3, '/(\\d+).*', 1, 1, 'e') as listing_id, SUM(TOTALHITS) as hits
FROM ga
WHERE PAGEPAGEPATHLEVEL2 = '/sales/'
GROUP BY (uid, listing_id)
And it works perfectly.而且效果很好。 However, once I pass the same query via python/pandas connection
但是,一旦我通过 python/pandas 连接传递相同的查询
pd.read_sql_query(query, con=con)
it also works, but all cells in listing_id
are empty, I assume something In the regex should be escaped, but can't find any documentation on that它也可以,但是
listing_id
中的所有单元格都是空的,我认为应该转义正则表达式中的某些内容,但找不到任何文档
You need to escape the backslashes in your Python code.您需要在 Python 代码中转义反斜杠。 Add a
r
before your string:在您的字符串之前添加一个
r
:
query = r"""
SELECT uid, REGEXP_SUBSTR(PAGEPAGEPATHLEVEL3, '/(\\d+).*', 1, 1, 'e') as listing_id, SUM(TOTALHITS) as hits
FROM ga
WHERE PAGEPAGEPATHLEVEL2 = '/sales/'
GROUP BY (uid, listing_id)
"""
or double the backslashes:或加倍反斜杠:
query = """
SELECT uid, REGEXP_SUBSTR(PAGEPAGEPATHLEVEL3, '/(\\\\d+).*', 1, 1, 'e') as listing_id, SUM(TOTALHITS) as hits
FROM ga
WHERE PAGEPAGEPATHLEVEL2 = '/sales/'
GROUP BY (uid, listing_id)
"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.