convert csv file to dbf
Asked Answered
P

1

6

i' ve got al lot of csv file and would like to convert them to a dbf file. I found the code from Ethan Furman (see below) It works really good - thanks a lot - but my csv files have as the delimiter a semicolon. So with the code python puts all my data into one column, but I've got 5 columns. How can I change the delimiter?

here the link: Convert .csv file into .dbf using Python?

especially:

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 filename parameter, 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.

Predicant answered 23/1, 2012 at 9:50 Comment(1)
I'm not sure what code you're referring to. It seems you're missing a few links. I checked the code at code.activestate.com/recipes/362715-dbf-reader-and-writer, and it seems to convert from one dbf (Xbase) format to another (binary), not from CSV to dbf. Is that the right code?Maternal
R
5

Looking at the dbf code, I don't see any way to pass a dialect, so you may transform your files as follows:

import csv
reader = csv.reader(open('input.csv'), delimiter=';')
writer = csv.writer(open('output.csv', 'w'))
for row in reader:
    writer.writerow(row)

Note: This will quote properly rows that already contain a comma as part of its contents.

Edit: If you're willing to patch dbf.from_csv to accept delimiter as a parameter to avoid transforming all your csv files, this should work:

--- dbf.py.orig 2012-01-23 12:48:32.112101218 +0100
+++ dbf.py  2012-01-23 12:49:59.468534408 +0100
@@ -4502,13 +4502,14 @@
         print str(table[0])
     finally:
         table.close()
-def from_csv(csvfile, to_disk=False, filename=None, field_names=None, extra_fields=None, dbf_type='db3', memo_size=64, min_field_size=1):
+def from_csv(csvfile, to_disk=False, filename=None, field_names=None, extra_fields=None, dbf_type='db3', memo_size=64, min_field_size=1,
+             delimiter=','):
     """creates a Character table from a csv file
     to_disk will create a table with the same name
     filename will be used if provided
     field_names default to f0, f1, f2, etc, unless specified (list)
     extra_fields can be used to add additional fields -- should be normal field specifiers (list)"""
-    reader = csv.reader(open(csvfile))
+    reader = csv.reader(open(csvfile), delimiter=delimiter)
     if field_names:
         field_names = ['%s M' % fn for fn in field_names]
     else:
Reamy answered 23/1, 2012 at 10:9 Comment(2)
Perhaps add 'import csv' just to make it completeShapely
@JohanLundberg I've added the missing import. Thanks for your feedback.Reamy

© 2022 - 2024 — McMap. All rights reserved.