首页 > 解决方案 > 转义数组中的项目(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 保留字上的错误

标签: pythonsqlpython-2.7sanitization

解决方案


使用最新的f字符串:

array = ['hello', 'world']
result = [f'`{item}`' for item in array]

print(result)
# ['`hello`', '`world`']

或者format

result = ['`{}`'.format(item) for item in array]

推荐阅读