Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting a .dbf file to a CSV file

581 views
Skip to first unread message

Johanna Pfalz

unread,
Nov 2, 2006, 12:33:08 PM11/2/06
to pytho...@python.org
Is there a module/method in python to convert a file from .DBF format to
.CSV format?

Johanna Pfalz

Max Erickson

unread,
Nov 2, 2006, 12:47:04 PM11/2/06
to pytho...@python.org
Johanna Pfalz <Johann...@telus.net> wrote:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362715

There is an example provided.

max

Tim Chase

unread,
Nov 2, 2006, 12:59:20 PM11/2/06
to Johanna Pfalz, pytho...@python.org
> Is there a module/method in python to convert a file from .DBF format to
> .CSV format?


Well, at

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362715

there's a recipe for reading DBF files...iterating over the
returned data from the function declared there, it should be
pretty easy to simply dump the results to a CSV file...something
like this untested:

outfile = file('out.txt', 'w')
for i, row in enumerate(dbfreader(file('x.txt'))):
if i == 1: continue # skip colinfo line
outfile.write('"')
outfile.write('","'.join(str(v) for v in row))
outfile.write('"\n')

Escaping quotes becomes mildly hairier, but only because there
are a number of ways to do it. You'd just modify the "str(v)"
with your escaping function...something like doubling them

str(v).replace('"', '""')
or
str(v).replace('\\', '\\\\').replace('"', '\\"')

using backslashes (where standalone-backslashes get escaped too)

Just a few idea,

-tkc

John Machin

unread,
Nov 2, 2006, 3:55:10 PM11/2/06
to
Johanna Pfalz wrote:
> Is there a module/method in python to convert a file from .DBF format to
> .CSV format?
>
>

Hi Johanna, the best start at the moment is the Python Cookbook recipe
that others have quoted. Steps:

(1) Input: as per recipe. Note that the recipe handles only the
earliest simplest form of dbf files; you may have data types other than
C, N, D and L. Note that there are myriads of third-party dbf writing
codes out there, and some of them are a little idiosyncratic. I am in
the pre-alpha stage of producing a module whose intent is to read just
about everything it is given, with lots of assertions, error checks,
and warnings. This includes M (memo) fields which are stored in a
separate file in 1 of at least 3 formats. If your file causes the
recipe to crashes or produce incorrect results, there is a good chance
that I may be able to help you.
(2) [optional] derive output file headings from names of fields.
(3) Convert data values to str, if str(input_value) is not appropriate
(4) Write values to csv file. For this I strongly suggest that you use
the Python csv module, rather than attempting to reinvent the wheel
(and the often-forgotten axle). If, as I guess from your name, that you
want the delimiter to be ";" instead of ",", then that can be handled
easily with ..., delimiter=";" in the call to csv.writer.

HTH,
John

0 new messages