-1

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,

El Mehdi OUAFIQ
  • 152
  • 1
  • 13
  • Would something as simple as casting it to a string and using a `.replace("\"", "'")` work? – Alfie Dec 16 '20 at 20:05
  • 1
    There should be no problem with a single quote if you feed in the values with `parameters` in the [`execute`](https://www.python.org/dev/peps/pep-0249/#id15) function. It seems like you try to construct your SQL string manually. – Matthias Dec 16 '20 at 20:13
  • The outermost quotes are not part of the value. `'XXX'` and `"XXX"` are the same string. You should just be able to use `if "'" in item:` to detect if a string contains a single quote. – John Gordon Dec 16 '20 at 20:19

1 Answers1

1

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')
gilch
  • 10,813
  • 1
  • 23
  • 28