Johanna Pfalz
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362715
There is an example provided.
max
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
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