Skip to main content
added 326 characters in body
Source Link
mr.adam
  • 3.2k
  • 14
  • 27

You are touching on a number of issues that are all related:

First, Python uses quotes to create strings. A string can be surrounded by either " or ', and whichever type is not used there can be used inside of the string without a problem. An escape character, as you note, can be used to allow for the outside quotation marks to be used inside the string.

Next, you are asking about field delimiters, i.e. the characters that are require by SQL to demarcate a field name. In your case, you are probably using shapefiles or feature classes. For those datasets, " " is the way to delimit fields. I think the brackets are for .mdb feature classes. If you ever have to write something to accommodate various types of datasets, it's good to use this tool.

Finally, to pass a variable into the query, it's very simple. Use python string formatting (because your "query" here is just a string, afterall):

qry = '"Nid" = \'{0}\''.format(AreaID[1])

EDIT: As @dmahr mentions .addFieldDelimiters() is a good thing to make into a habit. You can use it with above formatting like so

field_name = "Nid" #(or 'Nid', doesn't matter at this point)
delim_name = arcpy.AddFieldDelimiters(shapefile_path,field_name)
qry '{0} = \'{1}\''.format(delim_name,AreaID[1])

You are touching on a number of issues that are all related:

First, Python uses quotes to create strings. A string can be surrounded by either " or ', and whichever type is not used there can be used inside of the string without a problem. An escape character, as you note, can be used to allow for the outside quotation marks to be used inside the string.

Next, you are asking about field delimiters, i.e. the characters that are require by SQL to demarcate a field name. In your case, you are probably using shapefiles or feature classes. For those datasets, " " is the way to delimit fields. I think the brackets are for .mdb feature classes. If you ever have to write something to accommodate various types of datasets, it's good to use this tool.

Finally, to pass a variable into the query, it's very simple. Use python string formatting (because your "query" here is just a string, afterall):

qry = '"Nid" = \'{0}\''.format(AreaID[1])

You are touching on a number of issues that are all related:

First, Python uses quotes to create strings. A string can be surrounded by either " or ', and whichever type is not used there can be used inside of the string without a problem. An escape character, as you note, can be used to allow for the outside quotation marks to be used inside the string.

Next, you are asking about field delimiters, i.e. the characters that are require by SQL to demarcate a field name. In your case, you are probably using shapefiles or feature classes. For those datasets, " " is the way to delimit fields. I think the brackets are for .mdb feature classes. If you ever have to write something to accommodate various types of datasets, it's good to use this tool.

Finally, to pass a variable into the query, it's very simple. Use python string formatting (because your "query" here is just a string, afterall):

qry = '"Nid" = \'{0}\''.format(AreaID[1])

EDIT: As @dmahr mentions .addFieldDelimiters() is a good thing to make into a habit. You can use it with above formatting like so

field_name = "Nid" #(or 'Nid', doesn't matter at this point)
delim_name = arcpy.AddFieldDelimiters(shapefile_path,field_name)
qry '{0} = \'{1}\''.format(delim_name,AreaID[1])
Source Link
mr.adam
  • 3.2k
  • 14
  • 27

You are touching on a number of issues that are all related:

First, Python uses quotes to create strings. A string can be surrounded by either " or ', and whichever type is not used there can be used inside of the string without a problem. An escape character, as you note, can be used to allow for the outside quotation marks to be used inside the string.

Next, you are asking about field delimiters, i.e. the characters that are require by SQL to demarcate a field name. In your case, you are probably using shapefiles or feature classes. For those datasets, " " is the way to delimit fields. I think the brackets are for .mdb feature classes. If you ever have to write something to accommodate various types of datasets, it's good to use this tool.

Finally, to pass a variable into the query, it's very simple. Use python string formatting (because your "query" here is just a string, afterall):

qry = '"Nid" = \'{0}\''.format(AreaID[1])