Python-查找并替换字符串中的单引号

时间:2019-06-10 13:19:02

标签: python string quotes regex-lookarounds

为了提供一些背景信息,我从API中提取了几个字段并插入到t-sql表中。

在它的结尾,我有一个字符串,它是SQL语句的INSERT部分。但是,某些字符串可以在字符串中包含单引号,因此我的手动格式插入内容有偏斜,下面的示例支持:

INSERT INTO mytable (int, str, str, str, str, int) VALUES (111, 'some string', 'some string's first letter', 'some string ', NULL, NULL)

如您所见,这部分是

'some string's first letter'

引号内的引号破坏了格式。我正在考虑在引号内找到单个引号的旁边添加另一个引号。像这样:

'some string''s first letter'

最后我要声明:

INSERT INTO mytable (int, str, str, str, str, int) VALUES (111, 'some string', 'some string''s first letter', 'some string ', NULL, NULL)

请让我知道是否需要更多信息。

谢谢。

0 个答案:

没有答案