-1

I have written a code for count and sum unique values. I am getting the unique values count on Field "SubtypeCD" by using following code only if I am using "row[0]" instead of "desc_Domain[row[0]]]".My fields are domain coded.I need sum also on shapelength().

import arcpy
import csv
import os
import io
from arcpy import env
env.overwriteOutput = True

mxd=arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
count=0
field = ['SubtypeCD']

with open(r'D:\result.csv','wb') as resultFile:
  wr = csv.writer(resultFile, dialect='excel')
  for lyr in arcpy.mapping.ListLayers(mxd,"",df):
  if lyr.name=="LV UG Electric Line Segment":
    flds = [f.name for f in arcpy.ListFields(lyr)]
    s = set(flds)
    print lyr.name
    if 'SubtypeCD' in s :
      wr.writerow([lyr.name]) 
      CountUnique = {}
      sumUnique={}
      desc_Domain = {key: value['Name'] for (key, value) in 
      arcpy.da.ListSubtypes(arcpy.Describe(lyr).catalogPath).iteritems()}

      with arcpy.da.SearchCursor (lyr,[ "SubtypeCD","Shape.STLength()"]) as 
       cursor:
          for row in cursor:
            if not row[0] in CountUnique.keys():
               CountUnique[desc_Domain[row[0]]] = 1
            else:
               CountUnique[desc_Domain[row[0]]] += 1                  
                sumUnique+=row[1]

       for key in CountUnique.keys():
          print str(key) + ":", CountUnique[key], "features"
          total=str(key) ,CountUnique[key],"Features"
          wr.writerow(total)
  • Domain value issued resolved.I have missed desc_Domain in If statement. if not desc_Domain[row[0]] in CountUnique.keys(): – Sibghat Oct 12 '17 at 9:27
  • Still stuck with sum – Sibghat Oct 12 '17 at 9:27
  • 1
    Please edit your question with updates as per your comments above – Midavalo Oct 19 '17 at 0:21
-2
values = [row[0] for row in arcpy.da.SearchCursor(lyr, field)]
uniqueValues = set(values)

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

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.