python - Concatenating strings containing many quotations results in slashes in output -


i trying build string needs contain specific double , single quotation characters executing sql expression.

i need output formatted this:

" "full_stree" = 'allendale rd' " 

where value of allendale rd variable defined through loop. in following code sample, variable tos trying pass query variable.

tos = "allendale rd" query = '" "full_stree" = ' + "'" + tos + "' " + '"' 

and when print value of query variable output:

'" "full_stree" = \'allendale rd\' "' 

the slashes causing query fail. tried using modulus operator pass value of tos variable, same results:

where = '" "full_stree" = \'%s\' "' % (tos) print '" "full_stree" = \'allendale rd\' "' 

how can string concatenated correct format, leaving slashes out of expression?

what seeing repr of string.

>>> s = '" "full_stree" = \'allendale rd\' "' >>> s       # without print console displays repr '" "full_stree" = \'allendale rd\' "' >>> print s # print string displayed " "full_stree" = 'allendale rd' " 

your real problem quotes @ beginning , end of where-clause.

this

query = '" "full_stree" = ' + "'" + tos + "' " + '"' 

should be

query = '"full_stree" = ' + "'" + tos + "'" 

it more written

query = """"full_stree" = '%s'""" % tos 

the arcgis docs recommend more this

dataset = '/path/to/featureclass/shapefile/or/table' field = arcpy.addfielddelimiters(dataset, 'full_stree') whereclause = "%s = '%s'" % (field, tos) 

arcpy.addfielddelimiters makes sure field name includes proper quoting style dataset using (some use double-quotes , use square brackets).


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -