4

I was just curious as to whats the best way within a python script to get the output a dbf file/ table while the input is a shapefile. (if possible which i'm assuming it is)

Is there a way of converting a shape file into a table, or by selecting the attribute table of the shapefile and just outputting that.

Or am i going to need to create a table and then import the values?

  • If you have the path to your shapefile stored as a string variable (say, path_to_shp), you can simply do path_to_dbf = path_to_shp.replace('.shp', '.dbf') and use the attribute table for that shapefile directly. – nmpeterson Mar 25 '14 at 20:15
  • if all you're looking to do is convert the dbf to a different format, Excel will read a dbf (though it won't write one in Excel 2007 and newer). You can read the dbf and write it as csv or any other Excel-compatible format. You can also convert from shapefile to csv with gdal_translate. – Llaves Mar 26 '14 at 2:49
10

This is a very simple implementation using Table to Table (Conversion).

import arcpy

ws = r'C:\temp'
file = r'C:\temp\poly.shp'

arcpy.TableToTable_conversion(file, ws, "outTable.dbf")

For an even more efficient pure python approach, you can copy the shapefile's .dbf file using shutil:

import shutil

file = r'C:\temp\poly.dbf'
outFile = r'C:\temp\poly_copy.dbf'

shutil.copyfile(file, outFile)
  • Wow, thank you very much! I didn't even know there was a tool for that! thanks again Aaron! Have a great day! – Steve Mar 25 '14 at 16:00
  • .dbf extension is not required for outTable. – Jio Aug 24 '15 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.