使用Python将.csv文件转换为.dbf?
如何使用 python 脚本将 .csv 文件转换为 .dbf 文件?我在网上找到了这段一段代码,但我不确定它有多可靠。有没有具有此功能的模块?
How can I convert a .csv file into .dbf file using a python script? I found this piece of code online but I'm not certain how reliable it is. Are there any modules out there that have this functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 dbf 包,您可以获得一个基本的 csv 文件,其代码类似于以下内容:
这将创建具有相同名称、字符或备注字段以及 f0、f1、f2 等字段名称的表。
对于不同的文件名,请使用
filename
参数,如果您知道字段名称,则可以还可以使用field_names
参数。此处提供了相当基本的文档。
披露:我是这个包的作者。
Using the dbf package you can get a basic csv file with code similar to this:
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 thefield_names
parameter.Rather basic documentation is available here.
Disclosure: I am the author of this package.
您在网上找不到任何可以读取 CSV 文件并写入 DBF 文件的东西,这样您就可以调用它并提供 2 个文件路径。对于每个 DBF 字段,您需要指定类型、大小和(如果相关)小数位数。
一些问题:
什么软件将使用输出的 DBF 文件?
不存在“唯一的”DBF 文件格式这样的东西。您需要 dBase III 吗? dBase 4? 7?视觉 FoxPro? ETC?
您需要写入的文本字段的最大长度是多少?您有非 ASCII 文本吗?
Python 是哪个版本?
如果您的要求很低(dBase III 格式,没有非 ASCII 文本,文本 <= 254 字节长,Python 2.X),那么您引用的食谱应该可以完成这项工作。
You won't find anything on the net that reads a CSV file and writes a DBF file such that you can just invoke it and supply 2 file-paths. For each DBF field you need to specify the type, size, and (if relevant) number of decimal places.
Some questions:
What software is going to consume the output DBF file?
There is no such thing as "the" (one and only) DBF file format. Do you need dBase III ? dBase 4? 7? Visual FoxPro? etc?
What is the maximum length of text field that you need to write? Do you have non-ASCII text?
Which version of Python?
If your requirements are minimal (dBase III format, no non-ASCII text, text <= 254 bytes long, Python 2.X), then the cookbook recipe that you quoted should do the job.
使用
csv
库从 csv 文件读取数据。第三方dbf
库可以为您编写dbf文件。编辑:最初,我列出了
dbfpy
,但上面的库似乎更积极已更新。Use the
csv
library to read your data from the csv file. The third-partydbf
library can write a dbf file for you.Edit: Originally, I listed
dbfpy
, but the library above seems to be more actively updated.据我所知,没有一个是经过精心打磨的。多年来,我不得不多次使用 xBase 文件,并且当我必须这样做时,我不断发现自己编写代码来完成它。在我的一个备份中的某个地方,有一个非常实用的纯 Python 库可以做到这一点,但我不知道它到底在哪里。
幸运的是,xBase 文件格式并不那么复杂。当然,您可以在互联网上找到该规范。乍一看,您链接到的模块看起来不错,但当然,在使用它之前请复制您正在使用的任何数据。
一个可靠的、读/写的、功能齐全的、具有所有附加功能的 xBase 库已经在我的 TODO 列表中有一段时间了……如果我幸运的话,我什至可能会在今年剩下的时间里得到它...(但遗憾的是,可能不是)。
None that are well-polished, to my knowledge. I have had to work with xBase files many times over the years, and I keep finding myself writing code to do it when I have to do it. I have, somewhere in one of my backups, a pretty functional, pure-Python library to do it, but I don't know precisely where that is.
Fortunately, the xBase file format isn't all that complex. You can find the specification on the Internet, of course. At a glance the module that you linked to looks fine, but of course make copies of any data that you are working with before using it.
A solid, read/write, fully functional xBase library with all the bells and whistles is something that has been on my TODO list for a while... I might even get to it in what is left this year, if I'm lucky... (probably not, though, sadly).
我在这里创建了一个 python 脚本。它应该可以针对任何 csv 布局进行定制。在此之前,您确实需要了解 DBF 数据结构。此脚本需要两个 csv 文件,一个用于 DBF 标头设置,另一个用于您的正文数据。祝你好运。
https://github.com/mikebrennan/csv2dbf_python
I have created a python script here. It should be customizable for any csv layout. You do need to know your DBF data structure before this will be possible. This script requires two csv files, one for your DBF header setup and one for your body data. good luck.
https://github.com/mikebrennan/csv2dbf_python