0

In order for a script tool I'm writing in ArcMap to read fields in a source CSV correctly as text, I need to automate the writing of a shema.ini file in the source directory. In order to do that I need to write a list of fields from an input table to a text file with sequential column names, i.e., "Col1", "Col2", etc. before each field name from the list. Is there a way to insert a sequential number function into a for loop expression that will increment each field name in the list?

addressTable = arcpy.GetParameterAsText(0)
inputDir = os.path.dirname(addressTable)
schemaFile = str(inputDir) + "\schema.ini"
fieldList = arcpy.ListFields(addressTable)

with open(schemaFile, 'w') as f:
    for field in fieldList:
       f.write("Col" + ???? + "=" + field.name + " Text Width 128\n")

This is an example of what I need to get to:

Col1=UID Text Width 128
Col2=Address Text Width 128
Col3=City Text Width 128
Col4=State Text Width 128
Col5=ZIP Text Width 128

1 Answer 1

0

You can index a counter. For example:

counter = 0
for i in range(5):
    counter +=1
    print('Col' + str(counter))

This code returns or prints out....

Col1
Col2
Col3
Col4
Col5

So in your case you can increment the counter by 1 for each field, convert that to a string and do the concantenation.

counter = 0
with open(schemaFile, 'w') as f:
    for field in fieldList:
       counter +=1
       f.write("Col" + str(counter) + "=" + field.name + " Text Width 128\n")
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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