Python 将值写入dbf文件

标签 python dbf

我正在使用这个 DBF 库 http://pythonhosted.org/dbf/

我向 DBF 表添加了一列,现在我需要在此列中添加值,但遇到问题。

import dbf
import random

db = dbf.Table('test.dbf')
with db:
    #db.add_fields('ClasseType C(10)')
    for record in db:
        dbf.write(record, data="test")

错误:

"Traceback (most recent call last):
  File "C:/Python/23/dbf/addField.py", line 9, in 
    dbf.write(record, data="test")
  File "C:\Anaconda2\lib\site-packages\dbf\ver_2.py", line 7990, in write
    gather(record, kwargs)
  File "C:\Anaconda2\lib\site-packages\dbf\ver_2.py", line 8245, in gather
    raise FieldMissingError(key)
dbf.ver_2.FieldMissingError: 'data:  no such field in table'

Process finished with exit code 1

最佳答案

dbf 模块似乎已更改。这是“我的新的和改进的”答案。原始答案如下。

新答案(2018年11月28日)

  • 我创建一个仅包含三个字段的表,然后以读写模式打开它,以便向其中添加三个记录。我打印记录来证明成功。
  • 我关闭表并以读写模式重新打开它,添加电话号码字段,然后继续使用电话号码修改每条记录。我再次打印记录以证明成功。
  • 我之前发现 with record as r 构造可以访问记录的各个字段。这次提醒我,字符串字段存储时右侧填空。这就是为什么我在 r.name.strip() 中使用 strip(),这样我就可以在字典中进行查找。

--

## code to work with dbf module  aenum-2.1.2 dbf-0.97.11

import dbf

table = dbf.Table('temptable', 'name C(30); age N(3,0); birth D')

print('db definition created with field names:', table.field_names)

table.open(mode=dbf.READ_WRITE)
for datum in (('John Doe', 31, dbf.Date(1979, 9,13)),('Ethan Furman', 102, dbf.Date(1909, 4, 1)),('Jane Smith', 57, dbf.Date(1954, 7, 2)),('John Adams', 44, dbf.Date(1967, 1, 9)),):
    table.append(datum)

print ('records added:')
for record in table:
    print (record)
    print ('-----')

table.close()

table.open(mode=dbf.READ_WRITE)

table.add_fields('telephone C(10)')

telephones = {'John Doe': '1234', 'Ethan Furman': '2345', 'Jane Smith': '3456', 'John Adams': '4567'}
for record in table:
    with record as r:
        r.telephone = telephones[r.name.strip()]

print ('updated records')
for record in table:
    print (record)
    print ('-----')

原始答案

您可以向 dbf 表添加一列,然后按照此脚本中演示的方式将值写入该表中的各个单元格。请注意,我已将相同的电话号码添加到所有单元格。

我首先创建该产品文档中提供的表格和内容。我验证 field_names 和表的内容是否符合预期。

>>> import dbf
>>> table = dbf.Table('temptable', 'name C(30); age N(3,0); birth D')
>>> table.field_names
['name', 'age', 'birth']
>>> table.open()
dbf.ver_33.Table('temptable.dbf', status=<DbfStatus.READ_WRITE: 2>)
>>> for datum in (('John Doe', 31, dbf.Date(1979, 9,13)),('Ethan Furman', 102, dbf.Date(1909, 4, 1)),('Jane Smith', 57, dbf.Date(1954, 7, 2)),('John Adams', 44, dbf.Date(1967, 1, 9)),):
...     table.append(datum)
... 
>>> for record in table:
...     record
...     
 John Doe                       3119790913
 Ethan Furman                  10219090401
 Jane Smith                     5719540702
 John Adams                     4419670109
>>> table.close()

然后,我重新打开表,添加电话号码列并验证新更新的 field_names 属性。正如预期的那样,新字段是空白的。

>>> table = dbf.Table('temptable.dbf')
>>> table.open()
dbf.ver_33.Table('temptable.dbf', status=<DbfStatus.READ_WRITE: 2>)
>>> table.add_fields('Telephone C(10)')
>>> table.field_names
['name', 'age', 'birth', 'telephone']
>>> for record in table:
...     record.name, record.birth, record.telephone
...     
('John Doe                      ', datetime.date(1979, 9, 13), '          ')
('Ethan Furman                  ', datetime.date(1909, 4, 1), '          ')
('Jane Smith                    ', datetime.date(1954, 7, 2), '          ')
('John Adams                    ', datetime.date(1967, 1, 9), '          ')

我多次尝试设置电话的单独值,但没有成功。其中一条诊断消息指示我使用涉及 with 的语法结构,而这个有效。 (顺便说一句,我为北美电话号码选择的字段长度太小。)

>>> for record in table:
...     with record as r:
...         r.telephone = 'xxx xxx xx'
...         
>>> for record in table:
...     record
...     
 John Doe                       3119790913xxx xxx xx
 Ethan Furman                  10219090401xxx xxx xx
 Jane Smith                     5719540702xxx xxx xx
 John Adams                     4419670109xxx xxx xx
>>> table.close()

关于Python 将值写入dbf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45868498/

相关文章:

r - 如何读取 dBase 文件的一部分

c# - 使用可从 Excel(或非 VFP)读取的 C# 代码创建 .DBF

C# - dbf 外部表不是预期的格式

python - 更改 python mechanize 中的链接

python - 如何编译用于 Python 的 Fortran 库? (f2py 可能不是一个选项)

python - 使用 Python 从设备获取 MAC 地址

python - 为 PySpark 捆绑 Python3 包导致缺少导入

python - 如何识别嵌套字典中的最高值键?

c# - Visual Studio 2010 和 FoxPro 9.0 运行时错误 : "Feature is not available."

r - 导入不带行分隔符的固定宽度数据文件