0

How can I include single and double quotes in the same string in python?

I know how to fix the problem if I am using only single quotes or double quotes. I am using the following code:

my_variable = """INSERT into 
mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O'neal''', '''"The Film Title"''')""".encode("utf-8").decode('unicode_escape')

print(my_variable)

I get this in return:

'INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") 
VALUES(\'\'\'O\'neal\'\'\', \'\'\'"The Film Title"\'\'\')'

What I need is this:

'INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") 
VALUES('''O''neal''', '''"The Film Title"''')'
Simonidas
  • 43
  • 6
  • 2
    You get that on print? Or just typing variable into interactive prompt? It seems to print just fine as far as I can tell. In interactive session without print it will call `__repr__()` and not `__str__()`, so you'd see the escape character in it. – Ondrej K. Feb 10 '19 at 19:48
  • I am using pyspark with python 3 and psycopg2 to populate a postgres database. The problems is that when I execute the query in the string it contains the escape characters and therefore doesn't work on Postgres side. This means the string variable seems to hold the escape characters there, not just the interpreter on print. – Simonidas Feb 10 '19 at 20:05
  • It prints fine for me too and it doesn't contain any escape characters. – Stop harming Monica Feb 10 '19 at 20:14
  • Does it print UpperAndLowerCaseColumn between double quote, O'neal between triple quotes and "The Film Title" between triple quotes? – Simonidas Feb 10 '19 at 20:27
  • [`INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O'neal''', '''"The Film Title"''')`](https://onlinegdb.com/r1AqVzdHE) – Stop harming Monica Feb 18 '19 at 11:21

2 Answers2

1

Try this:

my_variable = """INSERT into mytable("UpperAndLowerCaseColumn","Second_Column")
VALUES(\'''O'neal\''', \'''"The Film Title"\''')""".encode("utf8").decode('unicode_escape')

print(my_variable)
Heyran.rs
  • 501
  • 1
  • 10
  • 20
  • How is that different from what I did? – Simonidas Feb 10 '19 at 20:08
  • I used \''' instead \'\'\' and went to next line before VALUES(... – Heyran.rs Feb 10 '19 at 20:12
  • Oh I see. It doesn't work. I get this: 'INSERT into mytable("UpperAndLowerCaseColumn","Second_Column")\nVALUES(\'\'\'O\'neal\'\'\', \'\'\'"The Film Title"\'\'\')' – Simonidas Feb 10 '19 at 20:14
  • No idea. I get this from my code: INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O'neal''', '''"The Film Title"''') – Heyran.rs Feb 10 '19 at 20:18
  • I'm sorry, I introduced a typo here when copying and pasting. This definitely works but now the question is, how can I introduce the escape character to a string that doesn't have it. Do you know how to do it? It's just because I am not declaring the variable manually. – Simonidas Feb 10 '19 at 20:31
  • let's say I want to generate that same string passing "UpperAndLowerCaseColumn","Second_Column", O''neal and "The Film" as variables. – Simonidas Feb 10 '19 at 20:35
0

Try this: my_variable = '''INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O'neal''', "The Film Title")'''.encode("utf-8").decode('unicode_escape')

Jeff
  • 57
  • 1
  • 2
  • 7
  • Isn't that what I did? – Simonidas Feb 10 '19 at 20:08
  • `my_variable` doesn't include a triple quote to close the statement. Triple quotes would be `''' string '''. Use this `my_variable = '''INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O'neal''', '''"The Film Title"''')""".encode("utf-8").decode('unicode_escape')''' ` – Jeff Feb 10 '19 at 20:14
  • Isn't that what I have? – Simonidas Feb 10 '19 at 20:16
  • It isn't, sorry, I see now. But the problem is that it is not generating what I need, which is this: 'INSERT into mytable("UpperAndLowerCaseColumn","Second_Column") VALUES('''O''neal''', '''"The Film Title"''')' – Simonidas Feb 10 '19 at 20:24
  • You have triple quotes after `film title` but not the end of the entire string. I think that is why your expected result is cut short. – Jeff Feb 10 '19 at 20:29
  • Try running this code, you'll see what I mean: table_name = "my_table" values_to_insert = ["""O'neal""", '''"The Film "'''] column_name_list = ["UpperAndLowercase", "otherColumn"] "INSERT INTO {} ".format(table_name) + ", ".join(['''"{}"'''.format(i) for i in column_name_list]) + "VALUES(" + ", ".join("""'''{}'''""".format(i).encode("utf-8").decode('unicode_escape') for i in values_to_insert) – Simonidas Feb 10 '19 at 22:00