I am using Make Table View to copy data from one table to a table view object and in the process hide and/or rename some fields. When I output the data to a dbf it hides and/or renames the fields accordingly. When I output the data to a geodatabase, it hides the specified fields but does not rename the ones that should be renamed. Any ideas why this is happening?
After some further testing, it looks like it is changing the field name, and not the field alias in the geodatabase. I tried changing field.name to field.aliasName, but that did not work.
import arcpy
from arcpy import env
# Set the enviroment
env.workspace = '//GIS-SDE/ParcelData.mdb'
env.qualifiedFieldNames = False
# Get the fields from the G_AccountInfo table in ParcelData.mdb
fields = arcpy.ListFields("G_AccountInfo")
# Create a fieldinfo object
fieldinfo = arcpy.FieldInfo()
# Iterate through the fields and set them to fieldinfo
for field in fields:
if field.name == "ACCOUNT_NUM":
fieldinfo.addField(field.name, "ACCOUNT", "VISIBLE", "")
elif field.name == "OWNER_NAME1":
fieldinfo.addField(field.name, "OWNERNAME", "VISIBLE", "")
elif field.name == "OWNER_ADDRESS_LINE1":
fieldinfo.addField(field.name, "OWNERADDRESS1", "VISIBLE", "")
else:
fieldinfo.addField(field.name, field.name, "HIDDEN", "")
arcpy.MakeTableView_management ("G_AccountInfo", "AccountInfo", '//GIS-SDE/ParcelData.mdb', fieldinfo)
# Give the output table a unique name
output = '//GIS-SDE/ParcelData.mdb/cleaned_data'
# Add the table back to the gdb
arcpy.CopyRows_management("AccountInfo", output)
-
When you make a copy of your table, do you want some fields to be removed/dropped, or just hidden? And others to be renamed, or just aliased?PolyGeo– PolyGeo ♦2013年04月26日 21:11:47 +00:00Commented Apr 26, 2013 at 21:11
-
Yes. I would like some fields removed, and the other fields I need them to show a different column header name in the output table (renamed).. I believe that would be alias, correct?user17596– user175962013年04月28日 03:18:35 +00:00Commented Apr 28, 2013 at 3:18
1 Answer 1
Try the test code below which should create a test file geodatabase table called xxx, with three fields (FieldA, FieldB, FieldC) and inserts a single row. It then uses FieldInfo, MakeTableView and CopyRows to output a dBase, personal geodatabase and file geodatabase table with FieldA renamed to FieldD and Field B removed. I think this is what you are trying to do. If this works for you then comment out the code to create test data and replace my test tables, fields, etc with yours.
import arcpy
if arcpy.Exists("C:/temp/test.gdb"):
arcpy.Delete_management("C:/temp/test.gdb")
if arcpy.Exists("C:/temp/test.mdb"):
arcpy.Delete_management("C:/temp/test.mdb")
if arcpy.Exists("C:/temp/test.dbf"):
arcpy.Delete_management("C:/temp/test.dbf")
arcpy.CreateFileGDB_management("C:/temp","test")
arcpy.CreatePersonalGDB_management("C:/temp","test")
arcpy.CreateTable_management("C:/temp/test.gdb","xxx")
arcpy.AddField_management("C:/temp/test.gdb/xxx","FieldA","TEXT")
arcpy.AddField_management("C:/temp/test.gdb/xxx","FieldB","TEXT")
arcpy.AddField_management("C:/temp/test.gdb/xxx","FieldC","TEXT")
rows = arcpy.InsertCursor("C:/temp/test.gdb/xxx")
row = rows.newRow()
row.FieldA = "Test1"
row.FieldB = "Test2"
row.FieldC = "Test3"
rows.insertRow(row)
del row,rows
intable = "C:/temp/test.gdb/xxx"
fields= arcpy.ListFields(intable)
fieldinfo = arcpy.FieldInfo()
for field in fields:
if field.name == "FieldA":
fieldinfo.addField(field.name, "FieldD", "VISIBLE", "")
elif field.name == "FieldB":
fieldinfo.addField(field.name, field.name, "HIDDEN", "")
elif field.name == "FieldC":
fieldinfo.addField(field.name, field.name, "VISIBLE", "")
arcpy.MakeTableView_management(intable, "xxx_view", "", "", fieldinfo)
arcpy.CopyRows_management("xxx_view", "C:/temp/test.dbf")
arcpy.CopyRows_management("xxx_view", "C:/temp/test.mdb/test")
arcpy.CopyRows_management("xxx_view", "C:/temp/test.gdb/test")
-
1This works correctly. What is interesting is that the field name displays as FieldD (the correct changed name) in ArcCatalog and in the .mdb when viewed in Access, but it still shows as FieldA when viewed in ArcMap. A bug maybe?user17596– user175962013年04月29日 15:16:51 +00:00Commented Apr 29, 2013 at 15:16