Append string containing parenthesis in Python


I’m writing a small script to generate a SQL INSERT statement. The data is pulled from a CSV file. I’m running into problems with generating the VALUES part of the statement where you have: (data,data,data),(data,data,data) …. etc

Code snippet:

values = []
data1 = <string>
data2 = <int>
data3 = <string>

values.append("('%s',%s,'%s')" % (data1,data2,data3))

Everything seems to work as planned, except when I print the contents of values, I get:


I don’t want the quotes surrounding the parenthesis because I’m trying to ultimately generate: VALUES (‘data1′,data2,’data3′),(‘data1′,data2,’data3′)….

If I remove the quotes in the values.append lines, the syntax breaks down. I’d rather not go through the ugly: values.append(“(‘” + data1 + “‘”…. setup if it’s possible to do it clean as originally planned. Just not sure how to deal with the parenthesis.

Link: Append string containing parenthesis in Python
Source: Stack Sql


About Author

Leave A Reply