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
Post a Comment