Select to view content in your preferred language

hide fields and export table through python

3773
9
Jump to solution
09-27-2017 06:45 AM
by Anonymous User
Not applicable

Hello.

I am building a script to run as an ArcGIS tool that takes a line file and does a bunch of stuff to it, then exports a table and uses that table to make route events.

I do not need, nor do I want, all of the fields from the original line file in the layer events attributes table. As such, I have been working on a way to export only the necessary fields from the feature class to a table for use in the make route even layers tool.

Here is what I have so far:

#Hide all fields that are not required for samples.
keepFieldList = ["SAMPLE_ID", "SEGMENT_ID", "NAME", "LENGTH", "WIDTH", "BEGIN_MP", "END_MP"]
fieldInfo = arcpy.FieldInfo()
fieldList = arcpy.ListFields(sampevents)
for field in fieldList:
    if field.name in keepFieldList:
        fieldInfo = fieldInfo + field.Name + " " + field.name + "VISIBLE;"
    else:
        fieldInfo = fieldInfo + field.Name + " " + field.name + "HIDDEN;"
       
#Export sample events to table for use in make route event layer.
arcpy.TableToTable_conversion(sampevents, gisdata, "Sample_Events_Table")

Here is the traceback info (Note that line 157 in the full script is line 9 in the above portion):

Traceback (most recent call last):
File "C:\Projects\CreateSamples\CreateSamples.py", line 157, in <module>
fieldInfo = fieldInfo + field.Name + " " + field.name + "HIDDEN;"
AttributeError: 'Field' object has no attribute 'Name'

Failed to execute (CreateSamples).

Thanks for any help resolving this. I am not entirely sure what the error is saying. My idea here is to hide all fields not in the keepFieldList and then export those fields to a new table. Then that table will be used as the event table input in the make route events tool.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

field.name and not field.Name .

This is a frustration of mine with Esri and ArcPy.  Python is case sensitive,  "name" is not the same as "Name" when naming variables, functions, methods, etc....  That said, there are some, but not all, ArcPy classes that allow for case insensitive calling of methods.  By not enforcing case sensitivity, ArcPy can teach new Python learners bad habits.

View solution in original post

9 Replies
JoshuaBixby
MVP Esteemed Contributor

field.name and not field.Name .

This is a frustration of mine with Esri and ArcPy.  Python is case sensitive,  "name" is not the same as "Name" when naming variables, functions, methods, etc....  That said, there are some, but not all, ArcPy classes that allow for case insensitive calling of methods.  By not enforcing case sensitivity, ArcPy can teach new Python learners bad habits.

by Anonymous User
Not applicable

Thanks! Yeah, I sort of knew that, but I am still fumbling through some of this stuff. Fixing the case to all lower got rid of this particular issue, but I ended up with a different error talking about operand types.

Traceback (most recent call last):
File "C:\Projects\CreateSamples\CreateSamples.py", line 157, in <module>
fieldInfo = fieldInfo + field.name + " " + field.name + "HIDDEN"
TypeError: unsupported operand type(s) for +: 'FieldInfo' and 'unicode'

Failed to execute (CreateSamples).

Seems that everything should be correct for the field info function, but maybe I am missing something in the syntax to update those...

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

FieldInfo—Help | ArcGIS Desktop  is an object with all sorts of methods.  The error message is saying that you can't concatenate a unicode object to a fieldInfo object.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Instead of trying to hide the fields for copy, can you use the field mapping in the table to table tool. That is how I do it. First do it manually once, you can see the syntax, and look at the help

Table To Table—Help | ArcGIS for Desktop 

Field Mapping (Optional)

The fields and field contents chosen from the input table. You can add, rename, or delete output fields as well as set properties such as data type and merge rule.

Learn more about choosing and setting the output fields.

You can use the ArcPy FieldMappings object for this parameter.

by Anonymous User
Not applicable

I've sort of looked into this, but it seems fairly overwhelming. I think this might be the ideal solution because it reduces code and sort of cleans things up, and takes care of this piece in a single step...I just don't really know what I am doing yet haha. I will try to dive in a little deeper on the field mapping stuff. Thanks!

0 Kudos
by Anonymous User
Not applicable

Rebecca, I was able to work out how to use this, and it worked perfectly, and lets me avoid using additional template files for another idea I had. Thank you!

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Glad it worked for you.  Never overlook what is already built into the tools, and as mentioned, test it manually, then right click on the results and choose to copy the python snippet so you can see what the format is they need. Keep in mind though, that sometimes they have a lot more in the snippet than you need. 

I typically drop any arguments at the end of the snippet that are default.  Then any in the middle that are default can sometimes just have a "#".  Also, usually you you don't need the "variablename = " portion, but I set the variables up before I use the tool and then just include those in the line.  Just some of the things I do, but everyone has their own workflow.   (and always remember that the print and arcpy.AddMessage are your friend.)

by Anonymous User
Not applicable

Thanks a lot for the advice Rebecca! Much appreciated!

0 Kudos
AhmadSALEH1
Frequent Contributor

Since Raifq didnt't mention how to solve the concatenation error (TypeError: unsupported operand type(s) for +: 'FieldInfo' and 'unicode')

I am adding the full correct answer just for future  documentation:

 

keepFieldList =['OBJECTID','f1','f2','f3']
Trees='D:/Test/1/x.gdb/Trees'

fieldList = arcpy.ListFields(Trees)
for field in fieldList:
    if field.name in keepFieldList:
        fieldInfo = fieldInfo + field.name + " " + field.name + " VISIBLE;"
    else:
        fieldInfo = str(fieldInfo) + str(field.name) + " " + str(field.name) + " HIDDEN;"‍‍‍‍‍‍‍‍‍‍‍‍‍‍

- change the field names to str

- add space before visible and hidden.

Thanks,