tags:

views:

66

answers:

1

Hello,

I would like to convert a csv file to dbf using python (for use in geocoding which is why I need the dbf file) - I can easily do this in stat/transfer or other similar programs but I would like to do as part of my script rather than having to go to an outside program. There appears to be a lot of help questions/answers for converting DBF to CSV but I am not having any luck the other way around.

An answer using dbfpy is fine, I just haven't had luck figuring out exactly how to do it.

As an example of what I am looking for, here is some code I found online for converting dbf to csv:

import csv,arcgisscripting

from dbfpy import dbf

gp = arcgisscripting.create()

try:

inFile = gp.GetParameterAsText(0) #Input

outFile = gp.GetParameterAsText(1)#Output

dbfFile = dbf.Dbf(open(inFile,'r'))

csvFile = csv.writer(open(outFile, 'wb'))


headers = range(len(dbfFile.fieldNames))


allRows = []

for row in dbfFile:

    rows = []

    for num in headers:

        rows.append(row[num])

    allRows.append(rows)


csvFile.writerow(dbfFile.fieldNames)

for row in allRows:

    print row

    csvFile.writerow(row)

except:

print gp.getmessage()

It would be great to get something similar for going the other way around.

Thank you!

A: 

For example, you could try:

You could also just open the CSV file in OpenOffice or Excel and save it in dBase format.

I assume you want to create attribute files for the Esri Shapefile format or something like that. Keep in mind that DBF files usually use ancient character encodings like CP 850. This may be a problem if your geo data contains names in foreign languages. However, Esri may have specified a different encoding.

EDIT: just noted that you do not want to use external tools.

Bernd Petersohn