我有一个带有这个结构的dbf文件:
__SAMPLEID
1
2
3
4
我想复制值但是更改了na列名,我期望的结果是这样的:
__SAMPLEID ID
1 1
2 2
3 3
4 4
我尝试在python上使用dbfpy或db libs,它可以做到吗?
答案 0 :(得分:1)
使用我的dbf library
:
重命名现有dbf中的字段:
import dbf
with dbf.Table('file-name-here.dbf') as table:
table.rename_field('old_name', 'new_name')
要复制表格,请为某些字段指定新名称:
import dbf
old_table = dbf.Table('original-table-here.dbf')
fields = old_table.structure()
# fields is a list of specifications, such as "name C(20)"
changes = [('field1', 'field10'), ('field2', 'field20')]
for old_name, new_name in changes:
for i, f in enumerate(fields):
if f.startswith(old_name + ' '): # note the space!
fields[i] = f.replace(old_name, new_name)
new_table = dbf.Table('new_table_name_here.dbf', fields)
with old_table as old, new_table as new:
for record in old_table:
new_table.append(tuple(record))