我有类似的东西:
from dbfpy import dbf
import random
db = dbf.Dbf('DMWWGS84/DMAWGS84.dbf',new=False)
db.addField(("Data","D"))
for record in db:
print record
record["Data"]=random.random()
db.close()
但它抱怨道:
Traceback (most recent call last):
File "merge_csv.py", line 5, in <module>
db.addField(("Data","D"))
File "/Users/alex/anaconda2/lib/python2.7/site-packages/dbfpy/dbf.py", line 246, in addField
raise TypeError("At least one record was added, "
TypeError: At least one record was added, structure can't be changed
它在谈论什么记录?这是一个很好的方法吗?
答案 0 :(得分:3)
请改用我的dbf库:
pip install dbf
(您可能需要先pip install enum34
)
然后是命令:
# lightly tested
import dbf
import random
db = dbf.Table('whatever.dbf')
with db:
db.add_fields('data N(12,7)')
for record in db:
dbf.write(record, data=random.random())
NB :将随机数写入日期字段不效果不错。
答案 1 :(得分:-1)
这个答案中有一个错误,你应该在使用之前打开文件:
import dbf
import random
db = dbf.Table('whatever.dbf')
db.open()
with db:
db.add_fields('data N(12,7)')
for record in db:
dbf.write(record, data=random.random())