python - 转义数组中的项目(Python)
问题描述
>>> array = ['hello', 'world']
>>> result = map(lambda item: `item`, array)
>>> result
["'hello'", "'world'"]
或者
>>> result = [`item` for item in array]
>>> result
["'hello'", "'world'"]
对于 sql,我需要用刻度转义的所有内容。
我正在寻找的结果是
["`hello`", "`world`"]
我这样做不是为了避免 SQL 注入,我这样做是为了避免 SQL 保留字上的错误
解决方案
使用最新的f
字符串:
array = ['hello', 'world']
result = [f'`{item}`' for item in array]
print(result)
# ['`hello`', '`world`']
或者format
:
result = ['`{}`'.format(item) for item in array]
推荐阅读
- c# - C# 线程安全类和列表
- java - 如何重用类实例化的逻辑并绕过 IllegalAccessException?
- python - python中浮点和整数的不同可用大小
- javascript - 禁用浏览器历史堆栈或强制重新加载 back()/forward()
- hadoop - distcp 本地文件到 hadoop
- active-directory - Microsoft ADCS:无法注册新的应用程序策略
- python - Python中表的成对行匹配
- excel - 如果前 8 行为空,则 ADODB 中的数据缺失
- python - 用 pandas 替换混合数据框中的异常值
- databricks - Databricks 上的集群是一个空屏幕?