Checking Data Types

312
4
Jump to solution
08-02-2021 05:13 AM
Timmon
by
New Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

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)

 

View solution in original post

0 Kudos
4 Replies
Luke_Pinner
MVP Regular Contributor

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)

 

View solution in original post

0 Kudos
Timmon
by
New Contributor

Thanks so much!

0 Kudos
emedina
New Contributor III

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.

 

 

0 Kudos
Timmon
by
New Contributor

I see so have the correct values I'm checking against in a list of objects to compare against. Thanks!

0 Kudos