尝试使用Python在shapefile表中提取每年不为零的最小值时遇到问题。我的桌子有“Station ID”,“Longitude”,“Latitude”和年份字段,以F为首字母。例如:F1970,F1971,F1972 ......我打算遍历每年的字段,然后将每年的所有值从最小值排序到最大值,然后得到第一个不等于零的记录,因为我只想要值大于零。到目前为止,我只能获得第一条记录,即零。谁能告诉我如何修改代码并获得第一个不为零的最小值?非常感谢!! [我希望获得与每年最小值相对应的车站ID,经度和纬度]
# Obtain the list of all fields in the attribute table of the shapefile "Station"
fields=arcpy.ListFields(Station)
# Construct a for loop to iterate through all the year attribute in the input feature class,in order to find the record with the minimum value in each year.
for field in fields:
year=str(field.name)
# find the year field
if ("F" in year):
where=year+" ASCENDING"
# Process: Sort
arcpy.Sort_management(Station, outputFC, where, "UR")
rows = arcpy.SearchCursor(outputFC)
row=rows.next
for row in rows:
# only got me the first record in each year, which is zero.
value=row.getValue(year)
stationID= row.getValue("Station_ID")
obsLon= row.getValue("Longitude")
obsLat=row.getValue("Latitude")
row=rows.next()
break
del row,rows
答案 0 :(得分:0)
您可以使用SearchCursor过滤行并对其进行排序。
# Obtain the list of all fields in the attribute table of the shapefile "Station"
fields=arcpy.ListFields(Station)
# Construct a for loop to iterate through all the year attribute in the input feature class,in order to find the record with the minimum value in each year.
for field in fields:
# find the year field
if ("F" in field.name):
year=str(field.name)
where_clause = year + " > 0"
sort_clause = year + " ASCENDING"
# Use a SearchCursor to filter and sort the rows
# Docs: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/SearchCursor/000v00000039000000/
rows = arcpy.SearchCursor(Station, where_clause, "", "", "", sort_clause)
# only got the first record in each year, which is > zero.
row = rows.next()
value=row.getValue(year)
stationID= row.getValue("Station_ID")
obsLon= row.getValue("Longitude")
obsLat=row.getValue("Latitude")
print "Record:", year, value, stationID, obsLon, obsLat
del row,rows