Python dbfpy和FoxPro

好吧,我在这里使用古老的数据库格式,dbf文件.不要问为什么,只知道某个软件决定扩展foxpro支持,因为微软决定扩展foxpro支持.现在,我在特定文件上收到以下错误.我已成功加载另一个文件,我很好奇这个数据库是否有问题.我敢肯定你可能需要查看数据库来确定这一点,但它的发布方式很大,所以我会采取我能得到的.

Traceback (most recent call last):
  File "billsapi.py", line 250, in <module>
    x.getUsedGuns()
  File "billsapi.py", line 72, in getUsedGuns
    itemdb = dbf.Dbf('item.dbf', readOnly=True, ignoreErrors=True)
  File "C:\Python27\lib\site-packages\dbfpy\dbf.py", line 135, in __init__
    self.header = self.HeaderClass.fromStream(self.stream)
  File "C:\Python27\lib\site-packages\dbfpy\header.py", line 127, in fromStream
    _fld = fields.lookupFor(_data[11]).fromString(_data, _pos)
  File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 455, in lookupFor
    return _fieldsRegistry[typeCode]
KeyError: '0'

继承我的简单代码返回此错误:

def getUsedGuns(self): 
    itemdb = dbf.Dbf('item.dbf', readOnly=True, ignoreErrors=True) 

就像我说的,我可以加载其他文件但没有问题,但也许有一个解决这个特定错误的工作?

编辑:我还想指出该文件可以在DBF View Plus中打开,查看和修改.

编辑:找到解决方案.我实际上最终使用了python dBase模块.我认为我的主要问题是没有备忘录文件(无论它们是什么,它都有.fpt文件扩展名).这是我目前正在使用的内容:

from dbf.tables import VfpTable
itemdb = VfpTable('item.db')
for rec in itemdb:
    print rec['MY_COLUM_NAME']

我还想指出,任何目前仍在使用FoxPro的人都应该被烧掉.

最佳答案 你的回溯是dbfpy告诉你你的文件有一个不受支持的dbfpy字段类型代码0的方式.它是一个Visual FoxPro(“VFP”)的东西.

这与备忘录文件无关.是的,如果有备注字段,则它们存储在.FPT文件中.访问foo.dbf时,foo.fpt需要存在.

你说“”“我实际上最终使用了python dBase模块”“”…大概你的意思是Ethan Furman的dbf模块,根据它的PyPI entry不支持空字段.

我有一个DBF阅读模块(pydbfrw),我一直想释放“其中一天”.以下是其文档的摘录:

Field Type      Description  DBF variety  Python 2.x type
0 (digit zero)  _NullFlags   VFP          N/A             

Notes: This field type is used only for the hidden _NullFlags field which
is a bit mask saying which fields in the record should be interpreted as NULL.

我的模块实现了识别并在需要时为字段值返回None.如果您需要该模块的副本,请查找我的电子邮件地址 – 例如google(“john machin xlrd”) – 给我发电子邮件,我会发给你.

点赞