我有一个SQL查询:
cur.execute("copy tmp from 's3://orders/20200515/orders.csv")
我如何参数化查询的/ 20200515 /部分?
类似:
list1=['20200515','20200516','20200517']
for A in list1:
cur.execute("copy tmp from 's3://orders/A/orders.csv")
您可以使用str.format
:
list1 = ['20200515','20200516','20200517']
for A in list1:
cur.execute("copy tmp from 's3://orders/{}/orders.csv".format(A))
请参阅official documentation了解更多信息。
自python 3.6起,您可以使用f字符串。 https://www.python.org/dev/peps/pep-0498/
list1 = ['20200515','20200516','20200517']
for A in list1:
cur.execute(f"copy tmp from 's3://orders/{A}/orders.csv")
F字符串比其他字符串格式化机制更有效