I am trying to build a tool that will generate a feature class and then populate the fields with the correct column name and format based off a Excel file. Has anyone had any luck doing this before? I'm looking for model or code examples if anyone happens to have anything similar. Has anyone seen any tutoria documents online that do this type of request? That could help also.
Thanks in advance!
This is certainly possible using Python. If you want to go in that direction, please post your starting script, or at least your Excel file as an example.
Thanks for the follow up Darren.
I have no code to share as I've been working in model builder. If you think the python route is the way to go I'm all ears. Here is what my Excel file looks like. I want column A to represent the fields in the new feature class and the values in column B should the the field type. Is this doable?
Step 1: convert Excel to CSV file. If you don't want to do that, you can work with Excel directly, but it's more complicated.
Step 2: run Python script. This will probably take some time to digest. Comments follow '#' (leave in or delete. They will be ignored).
import arcpy, csv, os fc = r'C:/junk/FILE_GDB.gdb/newEmptyFC' # point to where you want your FC if arcpy.Exists(fc): arcpy.Delete_management(fc) # delete FC if it exists arcpy.CreateFeatureclass_management(os.path.dirname(fc), os.path.basename(fc)) # create FC fieldtypes = { 'char':'TEXT', 'inte':'LONG'} # dictionary to translate from your listed data types to ArcGIS data types. You'll have to add more. with open(r'C:/junk/excel_test.csv', 'rb') as csvfile: # open CSV file reader = csv.reader(csvfile, dialect='excel') # make a CSV reader headers = reader.next() # skip header row for row in reader: # read through rows if row[0] <> 'OBJECTID': # don't create OID field. Will be created automatically print fieldtypes[row[1][:4]] # convince yourself that it is using the dictionary properly arcpy.AddField_management(fc, row[0], fieldtypes[row[1][:4]]) # create field of appropriate data type, based on first 4 characters in CSV file
Darren-I've had more time to digest this and I had some simply syntax errors on the front end. After I got those resolved it crashed out on me at the second to last line. Any ideas on what i might change?
Looks like an indentation error you would need to set your print statements to be in your if statement
It runs but I get this error now....any ideas?
Traceback (most recent call last):
File "C:/Users/tlathrop/Desktop/TL GIS/EnterpriseGeoDatabase/TestScript.py", line 16, in <module>
print fieldtypes[row[1][:4]]
KeyError: 'inte
I was being kind of lazy making the dictionary because I wasn't quite sure how your data looked.
fieldtypes[row[1][:4]]
...means: take the first four letters of the value in row[1], find the dictionary key that matches that value, and return the value at that key in the dictionary. Since there is no such value in your dictionary, it returns an error. When I made the example dictionary, I shortened the dictionary keys to four letters ("integer" became "inte") so there would be matches.
I understand the dictionary (I think o_O). I think it loos correct it makes sense but I'm still getting errors.
Here is my code:
Here is the error I get:
Any idea what I need to change? I appreciate your help too btw!!!
Ah, this one's easy. Python is case sensitive - it should be AddField, not Addfield.
The script will run but its not generating the data the way I had hoped. It doesn't look like any of the values in column A in the CSV file were added as column just the values for column B. Here is what the attribute table for the new FC looks like after running the tool:
I want the new FC to have columns based on the data in column A.
What do I need to change??
I created a small/quick/convenient model for myself.
----
Hmmm. Is this in the right place?
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.