2
arcpy.MakeFeatureLayer_management("N05-12_Station2_JGD.shp","station_12")

year=2012

arcpy.SelectLayerByAttribute_management("station_12","#",'"closing">=%d AND "opening"<=%d' % year)

Traceback (most recent call last):File "", line 1, in arcpy.SelectLayerByAttribute_management("station_12","#",'"closing">=%d AND "opening"<=%d' % year) TypeError: not enough arguments for format string

I want to select records that closing>=2012 and opening<=2012.

Above script, it occurs an error.

How can I write right script?

3

The problem is how you define the date. Dates in file geodatabases, shapefiles, and coverages are preceded with date. In Esri Help, you can find examples on usage.

"Datefield" = date 'yyyy-mm-dd'

import arcpy
arcpy.env.overwriteOutput = True
in_fc = r"C:\GIS\Temp\LayerB_shp.shp"
arcpy.MakeFeatureLayer_management(in_fc,"station_12")
year="date '2012-01-01'"
arcpy.SelectLayerByAttribute_management("station_12","#",""""closing">={0} AND "opening"<={0}""".format(year))
print int(arcpy.GetCount_management("station_12").getOutput(0))
1

I believe that substitution method has been deprecated, use format instead:

arcpy.SelectLayerByAttribute_management("station_12","#",'"x">={0} AND "y"<={0}'.format(year))
1

The "TypeError: not enough arguments for format string" is caused by the var substitution expecting 2 params - the 'format' command will 'map' your substitutions by index in the tuple specified. To write a similar query via the % operator, it is still supported using ArcGIS 10.2.x, however make sure you understand a string (%s) is being constructed (so you cannot use %d). Consider this command comparison testing within IDLE:

>>> year="date '2012-01-01'"
>>> print year
date '2012-01-01'

>>> '"closing">={0} AND "opening"<={0}"'.format(year)
'"closing">=date \'2012-01-01\' AND "opening"<=date \'2012-01-01\'"'

>>> '"closing">=%d AND "opening"<=%d' % (year,year)
Traceback (most recent call last):
  File "<pyshell#15>", line 1, in <module>
    '"closing">=%d AND "opening"<=%d' % (year,year)
TypeError: %d format: a number is required, not str

>>> '"closing">=%s AND "opening"<=%s' % (year,year)
'"closing">=date \'2012-01-01\' AND "opening"<=date \'2012-01-01\''
>>> 

Incidentally, in case you were wondering about the data type storage itself in the file gdb (10.2.x), then check this out - it's accessible via Python's datetime object:

>>> # using the da cursor to examine and modify datetime objs
>>> rows = arcpy.da.UpdateCursor("testAliasName",['UpdateOn','EndDate'])
>>> row = rows.next()

>>> # the row listing returned of 2 date fields- these are null values
>>> print row
[datetime.datetime(1899, 12, 30, 0, 0), datetime.datetime(1899, 12, 30, 0, 0)]

>>> # using a tuple to set date values
>>> row[0] = datetime.datetime(2011, 12, 1, 0, 0)
>>> row[1] = datetime.datetime(2012, 3, 1, 0, 0)
>>> rows.updateRow(row)
>>> del row, rows

>>> # now do the selection using your query format
>>> year = "date '2012-01-01'"
>>> print year
date '2012-01-01'

>>> # for convenience, setting up the query as a var
>>> query = '"EndDate" >= %s AND "UpdateOn" <= %s' % (year, year)
>>> print query
"EndDate" >= date '2012-01-01' AND "UpdateOn" <= date '2012-01-01'

>>> # the select by attribute using the formed query
>>> arcpy.SelectLayerByAttribute_management("testAliasName", "NEW_SELECTION", query)
<Result 'testAliasName'>
>>>  # works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.