Python, detect a character with on quote inside a string between double quotes

245 Views Asked by At

I have the example bellow of a tuple:

('XXXX','XXXX',"XXXX's Something",'XXXX')

I want to turn it into:

('XXXX','XXXX','XXXX''s Something','XXXX')

The reason why i'm trying to do it, is that this input will be ingested in Azure SQL database which skips the quote by using another single quote (as shown above).

How can I detect only the single quotes that are inside characters between double quotes?

Best regards,

1

There are 1 best solutions below

0
On BEST ANSWER

You can make a tuple comprehension with tuple() and a generator expression.

>>> t = ('XXXX','XXXX',"XXXX's Something",'XXXX')
>>> tuple(s.replace("'", "''") for s in t)
('XXXX', 'XXXX', "XXXX''s Something", 'XXXX')