Python新手在这里,所以请温柔。
我正在尝试将 dbf 文件转换为 csv。我偶然发现了一个以前的类似问题(虽然有相反的转换)并且正在考虑使用dbf模块。
使用文档中的示例,我尝试了此代码:
import dbf
dbf_fn = r'_PATH_HERE_\dbffile.dbf'
csv_fn = r'_PATH_HERE_\csvfile.csv'
in_db = dbf.Table(dbf_fn)
in_db.export(filename=csv_fn, header=True)
当我尝试运行它时,我收到以下错误:
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
in_db.export(filename=csv_fn, header=True)
File "C:\Python27\ArcGIS10.1\lib\site-packages\dbf.py", line 3379, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Db3Table' object has no attribute 'export'
为什么导出失败?
更新:
正如chm所建议的,我现在使用:
dbf.export(in_db, csv_fn, header=True)
这仍然会导致问题:
Traceback (most recent call last):
File "D:\Data_RP\data\projects\kN\GIS\python\04_convert_dbf_ALT.py", line 31, in <module>
dbf.export(in_db, filename=csv_fn, header=True)
File "C:\Python27\ArcGIS10.1\lib\site-packages\dbf.py", line 5670, in export
table = source_table(table_or_records[0])
File "C:\Python27\ArcGIS10.1\lib\site-packages\dbf.py", line 3384, in __getitem__
return self._table[value]
File "C:\Python27\ArcGIS10.1\lib\site-packages\dbf.py", line 3175, in __getitem__
raise DbfError("%s is closed; record %d is unavailable" % (meta.filename, index))
DbfError: D:\Data_RP\data\projects\kN\GIS\shapes\SWM_4N.dbf is closed; record 0 is unavailable