Single quotes being swapped for double quotes upon replace

39 Views Asked by At

I have a pandas DataFrame that looks like this

        orig_id modified_id original_term                   modified_term
1929    3668340 3578283     Advocate for clients'' needs.   Advocate for clients'' needs

You can see that I'm escaping the single quote (the reason being is that these strings are going to end up being part of a PostgreSql statement. I do this with

matches['original_term'] = matches['original_term'].map(lambda x: x.replace("'", "''"))
matches['modified_term'] = matches['modified_term'].map(lambda x: x.replace("'", "''"))

In order for this to be part of a SQL string, I convert these columns into a string of tuples, like so

orig_mod_pairs = str(tuple(zip(matches['original_term'], matches['modified_term'])))[1:-1]

This works well for all strings except the escaped ones. For instance, I see something like

('Advocacy and self-empowerment.', 'Advocacy and self-empowerment'), ('Advocacy and support services.', 'Advocacy and support services'), ('Advocacy for accessible and equitable mental health services.', 'Advocacy for accessible and equitable mental health services'), ('Advocacy groups.', 'Advocacy groups'), ("Advocate for clients'' needs.", "Advocate for clients'' needs")

But, as you can see, when we get to the escaped string, the single quotes around that substring "magically" turn into double quotes. What I want is to see single quotes throughout except for the escaped single quote. Something like

('Advocate for clients'' needs.', 'Advocate for clients'' needs')

Otherwise, PostgreSQL throws an error.

Ultimately, these tuples end up here

update_level1_string = f"""UPDATE associations_1
                           SET level_1_term = pairs.modified_term
                           FROM (VALUES {orig_mod_pairs}) as pairs (original_term, modified_term)
                           WHERE level_1_term = pairs.original_term;"""
0

There are 0 best solutions below