Python – Converting CSV to DBF

arcgis-10.1arcpypythontable

I have a python created CSV that I am looking to turn into a dbf but it is not getting read properly by the TABLE TO TABLE conversion tool. I select the csv, set the output to be XXX.dbf but the output has 1 field. It contains the number of rows I need, but the values of each row is the comma separated data. I have no spaces or special characters.

Arcgis 10.1
enter image description here

Here is the PYTHON used to create the table. This is my first time using the CSV writer; maybe I am implementing it wrong to feed the dbf.

#This script looks in a defined folder for shapefiles and creates the beginnings of a data dictionary.
# Creator: David Campbell
# Date: March 12, 2015

import arcpy, os, sys, csv,datetime, time
from arcpy import env

print 'Start @ ' + datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M:%S')

############## User Values #############
project = 'P747'
TSA_Table = 'TSA_LB_V2'
CLS_Table = 'CLS_LB_V2'
MDL_Table = 'MDL_LB_V2'
ARIS_Table = 'ARIS_V5'
RSA_Table = 'RSABLKS_V5'
AVI_Table = 'AVI_ATTR_V2'
FolderLocation = r"\\silver\clients\Projects\P747\3_Landbase\LB1\Submission_Data\LB_20150304"
LargestNumberForUniqueValues = 20
SDELocal = "C:\\Users\\david\\AppData\\Roaming\\ESRI\\Desktop10.1\\ArcCatalog\\TFC.sde\\"
Project_DB = "C:\\Users\\david\\AppData\\Roaming\\ESRI\\Desktop10.1\\ArcCatalog\\P747.sde\\"
ORA = 'ORATFC01'
#######################################

open(os.path.join(FolderLocation, "DD.csv"), "w")
FCView = FolderLocation + "\\FCView.lyr"
env.workspace = FolderLocation
SDELOC = SDELocal

if arcpy.Exists(FolderLocation + "\\DD.csv"):
    arcpy.Delete_management(FolderLocation + "\\DD.csv")

# if arcpy.Exists(SDELOC + "DD_Table") == False:
#     print "SDE Table does not exist"


if arcpy.Exists(SDELOC + "DD_Table"):
    arcpy.Delete_management(SDELOC + "DD_Table")

if arcpy.Exists(SDELOC + "DD_Table") == True:
    print "True"
    sys.exit()

a = "ID,PROJECT_NO,DATA_NAME,DATA_TYPE,DATA_LOCATION,SOURCE_CONTACT_NAME,PROJECTION,DATUM,UNITS,DATA_PRECISION,TOLERANCE,SCALE_OF_CAPTURE,GENERATION_ORIGIN,FEATURE_DESC,COLUMN_NAME,FLDTYPE_NAME,COLUMN_WIDTH,COLUMN_DECIMAL,COLUMN_DESC"
csvfile = FolderLocation + "\\DD.csv"
allrows = []
allrows.append(a)
SDETable = [TSA_Table, CLS_Table, MDL_Table, ARIS_Table, AVI_Table, RSA_Table]
#SDETable = ['TSA_LB_V2','CLS_LB_V2', 'MDL_LB_V2','ARIS_V5', 'RSABLKS_V5', 'AVI_ATTR_V2']

x = 1
for r in arcpy.ListFiles("*.shp"):

    if arcpy.Exists(FCView):
        arcpy.Delete_management(FCView)
    arcpy.MakeTableView_management(r, FCView)

    fields = arcpy.ListFields(r)
    desc = arcpy.Describe(r)

    for field in fields:
        FN = field.name
        DataString = str(x) + "," + project + ","+ r + "," + desc.shapeType + "," + FolderLocation + "," + "," + desc.spatialReference.name + "," + desc.spatialReference.GCS.datumName + "," + "Meters" + "," + "," + "," + "," + "," + "," + FN + "," + field.type + "," + str(field.length) + "," + str(field.precision)
        if field.type in ('String', 'SmallInteger'):
            UniqueValues = sorted(set([XX[0] for XX in arcpy.da.SearchCursor(FCView, FN)]))

            if int(len(UniqueValues)) >= LargestNumberForUniqueValues:
                b = DataString + ", Variable values"
                allrows.append(b)
                x += 1
            if int(len(UniqueValues)) < LargestNumberForUniqueValues:
                for XX in UniqueValues:
                    if XX == ' ':
                        continue
                    c = DataString + ", " + str(XX)
                    allrows.append(c)
                    x += 1
        else:
            e = DataString + ", 0 - X"
            allrows.append(e)
            x += 1


env.workspace = Project_DB
for SDE in SDETable:
    for SDEField in arcpy.ListFields(SDE):
        SDE_FN = SDEField.name
        ORASDEString = str(x) + "," + project + "," + str(SDE) + ",SDE Table" + "," + ORA + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + SDE_FN + "," + SDEField.type + "," + str(SDEField.length) + "," + str(SDEField.precision)

        if SDEField.type in ('String', 'SmallInteger'):
            UniqueValues2 = sorted(set([Y[0] for Y in arcpy.da.SearchCursor(SDE, SDE_FN)]))

            if int(len(UniqueValues2)) >= LargestNumberForUniqueValues:
                f = ORASDEString + ", Variable values"
                allrows.append(f)
                x += 1
            if int(len(UniqueValues2)) < LargestNumberForUniqueValues:
                for XXX in UniqueValues2:
                    g = ORASDEString + ", " + str(XXX)
                    allrows.append(g)
                    x += 1
        else:
            h = ORASDEString + ", 0 - X"
            allrows.append(h)
            x += 1

with open(csvfile, "w") as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in allrows:
        writer.writerow([val])

env.workspace = SDELOC

if arcpy.Exists(FCView):
    arcpy.Delete_management(FCView)
arcpy.MakeTableView_management(csvfile, FCView)
arcpy.TableToTable_conversion(FCView, SDELOC,  "DD_Table")
print 'Completed @ ' + datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M:%S')

Best Answer

You have quotes at the start and end of every line, and none in between, so ArcGIS is interpreting each line as a single text field. Remove the quotes and you should be ok in reading the file.

Otherwise if you want a programmatic solution to building the DBF file take a look at the Python dbf module for writing the DBF file. Using dbf.from_csv will quickly convert a CSV to a DBF file in memory wih

table = dbf.from_csv(path)

Or write directly to a file with the filename parameter

dbf.from_csv(path, "out_path.dbf")

This is unlikely to do precisely what you want though as it assumes that the first row of the CSV is not headers, but rather data, and that all fields are string fields. For something more complex you should work with the inbuilt Python csv module to read the data, and specify the table format yourself before writing the data.

Related Question