简体   繁体   中英

convert csv file to dbf

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 .

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM