[英]Python Write value in dbf file
我正在使用這個 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), ' ')
我曾多次嘗試設置telephone
個別值,但均未成功。 一條診斷消息指出我使用涉及with
的句法結構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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.