This code takes a .csv file, organizes the data and outputs another csv file that is used to create a .dbf file for ArcMap 10.4. The code appends the data as a float into the list 'output' and that list is written to the csv. However, the column that is 'begMi' in the .dbf, ends up being of datatype string, not float. Why is that happening?

Here is the code:

env.workspace = 'C:\Users\U2970\Desktop\SEC_FILE_EXPORTS'

def headerBody():
    output = []
    outfile = csv.writer(open('program_NORTH_data.csv','wb'))
    with open('NORTH_V2017.SEC') as line:
        for data in line:
            col = data.split('!')
            corrRB = col[4]+col[32]
            begMi = round(float(col[12]),3)
            endMi = round(float(col[13]),3)
            dir_ = col[16]
            lane = int(col[31])
            set_ = int(col[41])

    columns = 'corrRB','begMi','endMi','dir_','lane','set_'
    for line in output:


n = 0
inTable = r'C:\Users\U2970\Desktop\SEC_FILE_EXPORTS\program_NORTH_data.csv'
outLocation = 'Somewhere deep in a sharedrive'
while True:
        outTable = 'secfileDBF'+str(n)
        arcpy.TableToTable_conversion(inTable, outLocation, outTable)
        n = n + 1
        outTable = 'secfileDBF'+str(n)

print outTable,'Has been created'

Here is a snippit of the .dbf file before I cleaned it up:

 OID    corrRB       length endMi  dir_lane set_
   0    C000001E    383.967 385.72  I   0   0
   1    C000001E    0.0 3.795   I   1   0
   2    C000001E    3.795   16.928  I   1   0
   3    C000001E    16.928  32.259  I   1   0
   4    C000001E    32.259  35.738  I   1   0
   5    C000001E    35.738  41.946  I   1   0
   6    C000001E    41.946  81.332  I   1   0

If you wrote to a dbf file directly through the arcpy.da module, that would allow you to define the field types before inserting the data.

Otherwise the TabletoTable tool lets you specify field mappings. I believe you can use this to force a specific type of field instead of what ArcMap chooses.

For reference: https://desktop.arcgis.com/en/arcmap/10.3/tools/conversion-toolbox/table-to-table.htm

An example of setting up a field map: Change Field Type using Field Mapping for List of Tables using Python

  • The code im using was originally used to clean up the dataset before bringing it into ArcMap as a GDB. So I figured I would use this code for the purpose of creating a DBF this way. BTW thank you for the response. – Thomas_ Feb 8 '18 at 21:15

Ok I figured it out all I did was make the changes to the schema.ini file for the name and the datatype of that column. So I changed the datatype from text to double and that did the trick.

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.