Convert .csv file into .dbf using Python?
Asked Answered
R

5

11

How can I convert a .csv file into .dbf file using a python script? I found this piece of code online but I'm not certain how reliable it is. Are there any modules out there that have this functionality?

Richmal answered 14/12, 2010 at 15:31 Comment(1)
Google tells me about fiby.at/dbfpy and pypi.python.org/pypi/dbf/0.88.16. The dbf format looks pretty simple, though; you should be able to check the code you posted yourself.Banana
S
6

You won't find anything on the net that reads a CSV file and writes a DBF file such that you can just invoke it and supply 2 file-paths. For each DBF field you need to specify the type, size, and (if relevant) number of decimal places.

Some questions:

What software is going to consume the output DBF file?

There is no such thing as "the" (one and only) DBF file format. Do you need dBase III ? dBase 4? 7? Visual FoxPro? etc?

What is the maximum length of text field that you need to write? Do you have non-ASCII text?

Which version of Python?

If your requirements are minimal (dBase III format, no non-ASCII text, text <= 254 bytes long, Python 2.X), then the cookbook recipe that you quoted should do the job.

Stain answered 15/12, 2010 at 22:48 Comment(1)
Yes, I understand a little better how .csv can be converted to dbf now. The software I am using it for is ArcGIS, however I can't find the spec of which .dbf format is used. There is no non-ASCII text, The python version is 2.6, and the max length of text field is 20 characters.Richmal
A
8

Using the dbf package you can get a basic csv file with code similar to this:

import dbf
some_table = dbf.from_csv(csvfile='/path/to/file.csv', to_disk=True)

This will create table with the same name and either Character or Memo fields and field names of f0, f1, f2, etc.

For a different filename use the filenameparameter, and if you know your field names you can also use the field_names parameter.

some_table = dbf.from_csv(csvfile='data.csv', filename='mytable',
        field_names='name age birth'.split())

Rather basic documentation is available here.

Disclosure: I am the author of this package.

Actinon answered 13/7, 2011 at 2:18 Comment(7)
worked fine for me. Just a note: the csv should not have a header in your example, otherwise the header gets parsed as a regular row.Crownwork
I'm a bit confused: I'm looking at dbf 0.96.005, but can't figure out decisively if reading & writing DBase IV files are supported or not. I'm using dbfpy now, but am eager to finally move away from Python 2...Misdemeanor
dBase IV is not yet supported.Actinon
@ Ethan I am getting error Lib\site-packages\dbf.py", line 3637, in add_fields raise FieldSpecError("Maximum field name length is 10. '%s' is %d characters long." % (name, len(name))) while writing dbf from csv. My CSV has header but field_names paremeter seems to be to not working could you help? –Boxberry
@SIslam: That looks like an old version. Try the latest: sudo pip install dbf --upgrade.Actinon
Thanks but it creates .dbt file? and .dbf file is empty with just headers?Boxberry
@SIslam: I think you need to ask a new question. Make sure to include the versions you are using and the results you are getting.Actinon
S
6

You won't find anything on the net that reads a CSV file and writes a DBF file such that you can just invoke it and supply 2 file-paths. For each DBF field you need to specify the type, size, and (if relevant) number of decimal places.

Some questions:

What software is going to consume the output DBF file?

There is no such thing as "the" (one and only) DBF file format. Do you need dBase III ? dBase 4? 7? Visual FoxPro? etc?

What is the maximum length of text field that you need to write? Do you have non-ASCII text?

Which version of Python?

If your requirements are minimal (dBase III format, no non-ASCII text, text <= 254 bytes long, Python 2.X), then the cookbook recipe that you quoted should do the job.

Stain answered 15/12, 2010 at 22:48 Comment(1)
Yes, I understand a little better how .csv can be converted to dbf now. The software I am using it for is ArcGIS, however I can't find the spec of which .dbf format is used. There is no non-ASCII text, The python version is 2.6, and the max length of text field is 20 characters.Richmal
C
5

Use the csv library to read your data from the csv file. The third-party dbf library can write a dbf file for you.

Edit: Originally, I listed dbfpy, but the library above seems to be more actively updated.

Contract answered 14/12, 2010 at 15:35 Comment(4)
Looks like a functional, if not very Pythonic, library. Am definitely adding that to my bookmarks---thanks for the link!Stoned
The biggest drawback appears to be terrible documentation, which is a problem with many open source projects.Contract
Sadly the documentation for dbf is so limited I can't even figure out how to use it. There are a few example lines of code (which make it look quite easy), but they don't work. Nevertheless, thanks for the answer!Richmal
this is really helpful, I used dbfpy.Marden
S
2

None that are well-polished, to my knowledge. I have had to work with xBase files many times over the years, and I keep finding myself writing code to do it when I have to do it. I have, somewhere in one of my backups, a pretty functional, pure-Python library to do it, but I don't know precisely where that is.

Fortunately, the xBase file format isn't all that complex. You can find the specification on the Internet, of course. At a glance the module that you linked to looks fine, but of course make copies of any data that you are working with before using it.

A solid, read/write, fully functional xBase library with all the bells and whistles is something that has been on my TODO list for a while... I might even get to it in what is left this year, if I'm lucky... (probably not, though, sadly).

Stoned answered 14/12, 2010 at 15:37 Comment(0)
L
2

I have created a python script here. It should be customizable for any csv layout. You do need to know your DBF data structure before this will be possible. This script requires two csv files, one for your DBF header setup and one for your body data. good luck.

https://github.com/mikebrennan/csv2dbf_python

Lunisolar answered 26/9, 2013 at 18:58 Comment(1)
Your example uses dbfpy, which unfortunately doesn't seem to be maintained and is not updated for use in Python3 - it uses CStringIO, a.o. But if one is OK with Python 2, this example should provide a jump start.Misdemeanor

© 2022 - 2024 — McMap. All rights reserved.