如何从oracle游标的输出中添加转义字符串?

2024-09-27 18:10:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在元组中有来自oracle的记录,我试图用python在snowflake中执行bulkinsert语句,但我面临转义字符问题。你知道吗

输入:

('Sample',1234,"Here is escape' need",9700,'final')

输出:

('Sample',1234,'Here is escape'' need',9700,'final')

我曾尝试使用正则表达式捕获双引号之间的字符串,并尝试使用replace函数,但没有成功。你知道吗

输入:

('Sample',1234,"Here is escape' need",9700,'final')

输出:

('Sample',1234,'Here is escape'' need',9700,'final')

Tags: samplehereis记录语句needfinaloracle
2条回答

你为什么要修改任何东西?元组可以通过参数绑定直接插入: 你知道吗

t = ('Sample',1234,"Here is escape' need",9700,'final')
cursor.execute("INSERT INTO MY_TABLE VALUES(:1,:2,:3,:4,:5)", [t])

您可以按coma拆分值,并在部分上迭代搜索带有双引号的部分并替换它们:

inp = "('Sample',1234,\"Here is escape' need\",9700,'final')"
out = inp
for s in inp.strip("()").split(","):
    if s.startswith("\"") and s.endswith("\"") and "'" in s:
        out = out.replace(s, "'" + s[1: -1].replace("'", "\"") + "'")

结果:

('Sample',1234,'Here is escape" need',9700,'final')

相关问题 更多 >

    热门问题