'How to merge a list of dbf tables into one in Python?
I want to merge a lot of dbf tables in my "Output" folder, into one table. Here is my code, but it doesn't work (error showed below).:
import arcpy
import os
arcpy.env.workspace="C:\\Users\\Desktop\\Betty\\Output"
listTable = arcpy.ListTables ()
arcpy.Merge_management (listTable, 'C:\\Users\\Desktop\\Betty\\Output\\ppt.dbf')
print "done!"
Traceback (most recent call last): File "C:\Users\caobaijing\Desktop\Betty\Python\MergeTables.py", line 7, in arcpy.Merge_management (listTable, "C:\Users\caobaijing\Desktop\Betty\Output\ppt.dbf") File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 3762, in Merge raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Datasets: Dataset **does not exist or is not supported Failed to execute (Merge).
Solution 1:[1]
The above answer didn't work for me, listTable kept coming back with a blank result. Using ListFiles with a *.dbf wildcard did the trick.
I used filezilla to download a bunch of zipped shapefiles from ftp://ftp2.census.gov/geo/tiger/TIGER2014/PLACEEC/
I uncompressed them into a single directory (C:/Temp) so that all shapefiles were in one folder. I have 7-zip, so I just selected all the zip files, right click -> 7-zip -> extract here.
import arcpy
arcpy.env.workspace = "C:/Temp"
files = arcpy.ListFiles ( "*.dbf")
arcpy.Merge_management ( files, "C:/Temp/output.dbf" )
I opened the output.dbf in excel, I had all my data together.
Solution 2:[2]
Given a folder with dbf files, merge all of them into a df:
import pandas as pd
from simpledbf import Dbf5
import glob
# list all files
path = r'./dbf_folder/'
files = glob.glob(path + "\*.dbf")
# init empty df
merge = pd.DataFrame()
for file in files:
# load dbf into pandas df
df = Dbf5(file).to_dataframe()
# append df
merge = merge.append(
df, ignore_index=True)
# save to excel?
#merge.to_excel(r'Merge.xlsx', index=False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SpiffyFuture |
| Solution 2 | ramm |
