我有这个脚本的dbf文件:
__SAMPLEID
1
2
3
4
我想复制值但更改na列名,我期望的结果是这样的:
__SAMPLEID ID
1 1
2 2
3 3
4 4
我尝试在python上使用dbfpy或db libs,可能这样做吗?
最佳答案
使用我的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))
关于python - 复制列以更改Dbf中的名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40474283/