TableToTable_conversion syntax on select fields using python

1429
2
Jump to solution
03-17-2013 12:34 PM
ClaudineSicker
New Contributor III
I want to copy a geodatabase table (Test) to a dbf table (Test.dbf) but am struggling with the field mapping. I have about ten fields but only want to export the City and Zip fields into the new Test.dbf file. I can get it to write all the fields to the new dbf file but am struggling with exporting just two. Help seemed to indicate this was possible but provided no examples.

Here is what I have:

arcpy.TableToTable_conversion("Test","C:\\TestData","Test.dbf",,(["City"],["Zip"]))


What am I doing wrong????????
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
I have about ten fields but only want to export the City and Zip fields into the new Test.dbf file. I can get it to write all the fields to the new dbf file but am struggling with exporting just two.


You could futz with the fieldmappings object (you can look that up in the help), but honestly the easiest way to do what you want is to create the output table with just the two fields you want, and use Append_management with the "NO_TEST" option to copy the two fields over. The fields that do not match (by name) will be ignored in the append.

arcpy.CreateTable_management(arcpy.env.workspace,"out.dbf") arcpy.AddField_management("out.dbf","City","Text",field_length=20) arcpy.AddField_management("out.dbf","Zip","text",field_length=9) arcpy.DeleteField_management("out.dbf","ID") # dummy field created by CreateTable arcpy.Append_management("in_table","out.dbf","NO_TEST")

View solution in original post

0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor
I have about ten fields but only want to export the City and Zip fields into the new Test.dbf file. I can get it to write all the fields to the new dbf file but am struggling with exporting just two.


You could futz with the fieldmappings object (you can look that up in the help), but honestly the easiest way to do what you want is to create the output table with just the two fields you want, and use Append_management with the "NO_TEST" option to copy the two fields over. The fields that do not match (by name) will be ignored in the append.

arcpy.CreateTable_management(arcpy.env.workspace,"out.dbf") arcpy.AddField_management("out.dbf","City","Text",field_length=20) arcpy.AddField_management("out.dbf","Zip","text",field_length=9) arcpy.DeleteField_management("out.dbf","ID") # dummy field created by CreateTable arcpy.Append_management("in_table","out.dbf","NO_TEST")
0 Kudos
ClaudineSicker
New Contributor III
You could futz with the fieldmappings object (you can look that up in the help), but honestly the easiest way to do what you want is to create the output table with just the two fields you want, and use Append_management with the "NO_TEST" option to copy the two fields over. The fields that do not match (by name) will be ignored in the append.

arcpy.CreateTable_management(arcpy.env.workspace,"out.dbf")
arcpy.AddField_management("out.dbf","City","Text",field_length=20)
arcpy.AddField_management("out.dbf","Zip","text",field_length=9)
arcpy.DeleteField_management("out.dbf","ID") # dummy field created by CreateTable
arcpy.Append_management("in_table","out.dbf","NO_TEST")


I figured I was going to have to do this, I was just hoping that the syntax for the Table to Table conversion was easier than I feared. Good thing there are multiple ways to do things in ArcGIS or I'd of put a bullet in my machine by now. :-).

**** Added note after running script ************

Field created was actually called "field1" not "ID".
ARCGIS automatically added OID field which I cannot delete (but want to) so I'm not actually getting a two field dbf but a 3 field.

I have to export this dbf as a csv. Maybe I can delete the OID field then. Any hints as to how to export dbf to csv (and maybe eliminate OID field?)
0 Kudos