Hi,
I'm trying to create a script that checks the column name and datatype. Right now I can grab the column name but would also like to return the datatype to check them both at the same time.
for table in tables:
fields = [f.name for f in arcpy.ListFields(os.path.join(location,prefix+table))]
if table == 'Customers':
for field in ['FirstName', 'LastName','Location']:
if field not in fields:
print(table + '.' + field + ' - Incorrect)
else:
print(table + '.' + field + ' - Correct')
I have a number of tables which I'm planning on running a check against for column name and datatype if possible. If anyone has done something similar or could help me with understanding how to check the datatype as well I would greatly appreciate it.
Solved! Go to Solution.
https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/listfields.htm
Returns a list of Field objects, you've grabbed the name property, but you can also access type:
for field in arcpy.ListFields(os.path.join(location,prefix+table)):
print(field.name, field.type)
https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/listfields.htm
Returns a list of Field objects, you've grabbed the name property, but you can also access type:
for field in arcpy.ListFields(os.path.join(location,prefix+table)):
print(field.name, field.type)
Thanks so much!
Hi,
The fields have properties you can access as mentioned here: https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/field.htm
So, for each field returned by ListFields, you want check that field.name is in ['FirstName', 'LastName','Location']?
From there, you want to do an additional check to see that field.type matches the corresponding field type?
I would probably modify the approach to make it a little easier. A simple list of dictionaries or objects will do. So you'd have [{"field": "FirstName", "type": "whatever"}, {"field": "LastName", "type": "whatever"}, {"field": "Location", "type": "whatever"}]. If you can get the field objects for the thing you're checking against that might be easier.
I see so have the correct values I'm checking against in a list of objects to compare against. Thanks!