python - Python中的转义+引号
问题描述
我想要做的是将转义字符放在 python 字符串中。让我解释。
我有字符串
str = 'station and the "Tren Bach"'
然后str
将其格式化为大的“SQL”查询,malformed exception
由于“Tren Bach”中间的双引号。
所以我尝试在格式化之前进行预处理,如下所示。
str = str.replace ('"', '\"')
sql.format(str)
但是在 sql 字符串中,没有逃逸。
其次我试过,
str = str.replace ('"', '\\"')
sql.format (str)
但是此时,sql中显示了双重转义。
我的预期如下。
str = 'station and the \"Tren Bach\"'
这样str
就不会在sql语句中出问题了。
你会推荐其他方式吗?
解决方案
BigQuery 使用与标准 SQL略有不同的转义。您需要在查询中使用引号将值括起来,类似于 Python。例如:
s = '''SELECT ... WHERE my_field='station and the "Tren Bach"' '''
# note quotes here ^ and here ^
推荐阅读
- android - 如何跟踪用户为 PagedListAdapter 中的每个项目所做的每个 API 调用的状态?
- vue.js - Vuex——全球处理网络繁忙状态?
- amazon-web-services - 您如何将 API 网关指向 CDK 中的 lambda 别名?
- php - 用正则表达式替换 PHP 代码
- sql - 返回不同的行,同时从不同的列中排除一列
- javascript - Moment.js 为字符串输入添加时间
- postgresql - 如何编写一个 postgresql 函数来汇总每日报告表中的分组指标
- c# - 如何合并列表
GroupBy() 中的字段 - reactjs - 如何断言包装在 debounce 中的函数被调用
- wordpress - Wordpress 下拉菜单在本地工作,但不在服务器上