DBFreader python 3.7 问题

DBFreader python 3.7 issues

我想在 Python 3.7 上使用 dbfread 模块使用 dbf 文件,它使用一个小的 dbf

from dbfread import DBF
from struct import *

table = DBF('usuarios.dbf', load=True)
for item in table:
    print (item)

输出:

OrderedDict([('NUMUSER', '    0'), ('NOMUSER', 'Rosy'), ('PASSWORD', ''), ('NIVEL', 'SUPER'), ('VALIDAR', 'P?@qMwá¿|Ew}"Q-JW0Q0:iw^'), ('EMAIL', 'MARLENGURROLA@gmail.com|'), ('MAILTIPO', 1), ('MAILFIRMA', None), ('MAILSMTP', 'Ghf2U*wT3Ik?D#>W0@+9@," ¡.deZ+%¿i?GL0oBrO+éZ=KwXw{E(LXIv#ñOññW+t"AruéñAm\O>YB$iTNv*\'Ñé2).*qv#88XZ5k%KK%R}~¡oOgiTó\'=#'), ('HUELLA1', None), ('HUELLA2', None), ('METODO', 0), ('ACTIVO', True)])
[Finished in 0.2s]

但是当我尝试使用大 dbf 时它显示错误

from dbfread import DBF
from struct import *

table = DBF('docum.dbf', load=True)
for item in table:
    print (item)

输出2:

Traceback (most recent call last):
  File "C:\Users\user\rdbfs.py", line 4, in <module>
    table = DBF('docum.dbf', load=True)
  File "C:\Python3\lib\site-packages\dbfread\dbf.py", line 121, in __init__
    self._read_header(infile)
  File "C:\Python3\lib\site-packages\dbfread\dbf.py", line 206, in _read_header
    self.header = DBFHeader.read(infile)
  File "C:\Python3\lib\site-packages\dbfread\struct_parser.py", line 41, in read
    return self.unpack(file.read(self.struct.size))
  File "C:\Python3\lib\site-packages\dbfread\struct_parser.py", line 36, in unpack
    items = zip(self.names, self.struct.unpack(data))
struct.error: unpack requires a buffer of 32 bytes
[Finished in 0.2s with exit code 1]

我不知道 python 中的打包或解包数据,你们能帮帮我吗?或给予指导。谢谢!

我自己不使用dbfread,所以我不知道为什么它不起作用。

您可以尝试使用我的库 dbf,它看起来像:

import dbf

table = dbf.Table('usuarios.dbf')
table.open()
for item in table:
    print item

record = table[0]  # first record
print record.numuser
print record.nomuser

有效!!

    import dbf

ruta = 'Y:/SAITBC/'
table = dbf.Table(ruta+'clientes.dbf')
tablita = table.open()
print(tablita)

#for item in tablita:
#   print(item)

record = tablita[0]
print (record['nomcli'])

输出:

    Table:         Y:/SAITBC/clientes.dbf
    Type:          Foxpro
    Codepage:      cp1252 (Windows ANSI)
    Status:        DbfStatus.READ_ONLY
    Last updated:  2018-12-20
    Record count:  1282
    Field count:   79
    Record length: 1230 
    --Fields--
      0) numcli C(5)
      1) nomcli C(200)
      2) calle C(60)
      3) numext C(10)
      4) colonia C(60)
      5) ciudad C(30)
      6) estado C(20)
      7) cp C(10)
      8) telefono C(30)
      9) fax C(20)
     10) clasif C(5)
     11) ventano N(12,2)
     12) ultvent D
     13) atvent C(40)
     14) atcobr C(40)
     15) rfc C(13)
     16) limcred N(12,2)
     17) saldo N(12,2)
     18) pjedesc N(5,2)
     19) diascred N(3,0)
     20) precioutil C(1)
     21) recepfac C(30)
     22) pagofac C(30)
     23) obs M
     24) email1 C(40)
     25) email2 C(40)
     26) numcta C(20)
     27) uid N(10,0)
     28) numvend C(5)
     29) obligareq L
     30) suspendido L
     31) bloqueasop L
     32) direnvio M
     33) otrosdatos M
     34) impuesto1 N(6,2)
     35) retencion1 N(10,4)
     36) retencion2 N(6,2)
     37) permitecod L
     38) llavecred L
     39) tiposop C(10)
     40) clavecli C(20)
     41) curp C(20)
     42) pais C(15)
     43) nomcomer C(40)
     44) cfgdatdoc M
     45) datosfe M
     46) statusweb N(1,0)
     47) claveweb C(32)
     48) numzona C(5)
     49) metodopago M
     50) metodousar C(2)
     51) tiposys C(10)
     52) licencia N(10,0)
     53) ncontra C(10)
     54) pass C(10)
     55) rsocial N(10,0)
     56) usanom L
     57) contnomi C(10)
     58) passnom C(10)
     59) numint C(10)
     60) cosac C(2)
     61) modi L
     62) altasg D
     63) usocfdi C(3)
     64) formapago C(2)
     65) fechanac D
     66) pjepuntos N(5,2)
     67) pjedescmes N(5,2)
     68) pjedescdia N(5,2)
     69) implocal M
     70) poliza C(10)
     71) vencsait D
     72) usaenl L
     73) vencnom D
     74) rsocnom N(10,2)
     75) usabov L
     76) vencbov D
     77) tiponom C(20)
     78) condpago C(30)

PEñUELAS TAPIA JAIME ROJO
[0.4 秒内完成]