Python 复制一列,更改Dbf中的名称

Python 复制一列,更改Dbf中的名称,python,dbf,Python,Dbf,我有一个包含以下内容的dbf文件: __SAMPLEID 1 2 3 4 我想复制这些值,但更改了na列名,结果如下: __SAMPLEID ID 1 1 2 2 3 3 4 4 我尝试在python上使用dbfpy或db libs,这是可能的吗?使用我的: 要重命名现有dbf中的字段,请执行以下操作: import dbf with dbf.

我有一个包含以下内容的dbf文件:

__SAMPLEID
  1
  2
  3
  4
我想复制这些值,但更改了na列名,结果如下:

__SAMPLEID       ID
  1              1
  2              2
  3              3
  4              4
我尝试在python上使用dbfpy或db libs,这是可能的吗?

使用我的:

要重命名现有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))

所以你想要一个新的数据库,它的值与原始数据库的值相同,但列名不同?实际上,可以在同一个数据库中并不重要