0

I was trying to make a script that counts the max lenght found in each field of a table with data in it, but it immediately crashes arcmap upon executing

I know there might be a tool available on the internet, but I need to learn how to do it myself

Parameter(0) is a table input

Parameter(1) is a multi select fields input from the table

Code :

#Created By: Gabriel Canto
#Date: Julho 2016

#Import modules
import arcpy

#Variables
table = arcpy.GetParameter(0)
fields = arcpy.GetParameterAsText(1).split(";")
fieldnames = {}
stringfield = ""

#Creating the fields parameter string
for field in fields:
    fieldlenghts[field] = 0
    stringfield = stringfield + "'" + field + "',"

#Removing the last comma
stringfield = stringfield[:-1]


with arcpy.da.SearchCursor(table,[stringfield]) as rows:
    for row in rows:
    for field in fields:
        try:
            s = str(row.getValue(field))
            if fieldlenghts[field] < len(row.getValue(field)):
                fieldlenghts[field] = len(row.getValue(field))
        except:
            arcpy.AddMessage("Field " + field + " can't be a string")


output = ""   
for field in fields:
    output = field + " : " + fieldlenghts[field] + " caracters\n"


arcpy.AddMessage(output) 
1

If you're looking for the length of a field, you can use arcpy.ListFields which returns a list of field objects that contain information about the field (List Fields: http://pro.arcgis.com/en/pro-app/arcpy/functions/listfields.htm , Field Objects: http://pro.arcgis.com/en/pro-app/arcpy/classes/field.htm ).

field_info = arcpy.ListFields(data)
for field in field_info:
    print field.name, 
    print field.length, 
    print field.type, 

If you're trying to see the longest input in a field, you need to iterate through the rows, not the fields. Think of a search cursor like a list of tuples, like [(field1, field2, field3), (field1, field2, field3)]. If this is your cursor, when you iterate in a for loop for row in cursor: your variable row is equal to (field1, field2, field3), so when you want to look at a specific field in a row, you reference the field's index value like row[0] which would return the value field1 in this example.

So, to help rework your example,

import arcpy

table = arcpy.GetParameter(0)
## Automatically get your field names
fields = [field.name for field in arcpy.ListFields(table)]

## We'll use a python dict to store the field name and the 
## length of the longest value
field_len_dict = {i:0 for i in fields}

with arcpy.da.SearchCursor(table, fields) as cursor:
    for row in cursor: ## Iterate through rows
        index = 0
        for field in row: ## iterate through each value of the row
            if field is not None:
                if len(field) > field_len_dict[fields[index]]:
                    field_len_dict[fields[index]] = len(field)
                    index +=1
                else:
                    index +=1
             else: index +=1

arcpy.AddMessage(field_len_dict)

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.