将 csv 文件转换为 dbf

发布于 2024-12-28 05:51:01 字数 850 浏览 1 评论 0原文

我有很多 csv 文件,想将它们转换为 dbf 文件。 我从 Ethan Furman 那里找到了代码(见下文) 它工作得非常好 - 非常感谢 - 但我的 csv 文件以分号作为分隔符。因此,使用代码 python 将所有数据放入一列中,但我有 5 列。 如何更改分隔符?

这里是链接: 使用 Python 将 .csv 文件转换为 .dbf?

特别是:

使用 dbf 包,您可以获得一个基本的 csv 文件,其代码类似于以下内容:

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

这将创建具有相同名称和字符或备注字段以及 f0、f1、f2 等字段名称的表。

对于不同的文件名,请使用 filename 参数,如果您知道字段名称,也可以使用 field_names 参数。

some_table = dbf.from_csv(csvfile='data.csv', filename='mytable',
        field_names='姓名 年龄 出生'.split())

此处提供了相当基本的文档。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2025-01-04 05:51:01

查看 dbf 代码,我没有看到任何传递方言的方法,因此您可以按如下方式转换文件:

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

注意:这将正确引用已包含逗号作为其一部分的行内容。

编辑:如果您愿意修补 dbf.from_csv 以接受 delimiter 作为参数以避免转换所有 csv 文件,这应该可行:

--- 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:

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:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文