[英]Python Write value in dbf file
I am using this DBF library http://pythonhosted.org/dbf/我正在使用这个 DBF 库http://pythonhosted.org/dbf/
I added a column to DBF table, now I need to add values in this column, but I am having problems.我在 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")
Error:错误:
"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
The dbf module appears to have changed. dbf 模块似乎已更改。 Here's 'my new and improved' answer.
这是“我的新的和改进的”答案。 The original answer appears below.
原始答案如下所示。
New answer (2018 11 28)新答案 (2018 11 28)
with record as r
construction makes it possible to access individual fields of a record.with record as r
构造可以访问记录的各个字段。 This time I was reminded that string fields are stored with blank fill on the right.strip()
in r.name.strip()
, so that I could do a lookup in the dictionary.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 ('-----')
Original answer原答案
You can add a column to a dbf table and then write values to individual cells in that table in the way demonstrated in this script.您可以向 dbf 表中添加一列,然后按照此脚本中演示的方式将值写入该表中的各个单元格。 Notice that I've add the same telephone number to all of the cells.
请注意,我已将相同的电话号码添加到所有单元格中。
I begin by creating the table and contents offered in the documentation for this product.我首先创建此产品文档中提供的表格和内容。 I verify that
field_names
and the contents of the table are what would be expected.我验证了
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()
Then I re-open the table, add a column for telephone numbers and verify the newly updated field_names
attribute.然后我重新打开表,为电话号码添加一列并验证新更新的
field_names
属性。 The new field is blank, as expected.正如预期的那样,新字段是空白的。
>>> 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), ' ')
I made several attempts to set individual values of telephone
without success.我曾多次尝试设置
telephone
个别值,但均未成功。 One of the diagnostic messages pointed me toward using a syntactic construction involving with
and this one works.一条诊断消息指出我使用涉及
with
的句法结构with
并且这个结构有效。 (Incidentally, the field length I chose for North American telephone numbers was too small.) (顺便说一句,我为北美电话号码选择的字段长度太小了。)
>>> 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.