dbf to csv - raise NotImplementedError

438 views
Skip to first unread message

Vinicius Henrique

unread,
Jun 28, 2016, 1:10:04 PM6/28/16
to Python dBase

Hey, guys!

I'm converting DBF file to CSV, but the script gives me the following error:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    for rec in in_db:
  File "C:\Python27\lib\site-packages\dbfpy\dbf.py", line 242, in __getitem__
    return self.RecordClass.fromStream(self, self._fixIndex(index))
  File "C:\Python27\lib\site-packages\dbfpy\record.py", line 121, in fromStream
    return cls.fromString(dbf, cls.rawFromStream(dbf, index), index)
  File "C:\Python27\lib\site-packages\dbfpy\record.py", line 140, in fromString
    [_fd.decodeFromRecord(string) for _fd in dbf.header.fields])
  File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 173, in decodeFromR
ecord
    return self.decodeValue(self.rawFromRecord(record))
  File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 342, in decodeValue

    raise NotImplementedError
NotImplementedError



I'm trying using this script:

 
import csv
from dbfpy import dbf
import sys

dbf_fn = sys.argv[1]
csv_fn = sys.argv[2]

in_db = dbf.Dbf(dbf_fn)
out_csv = csv.writer(open(csv_fn, 'wb'))

names = []
for field in in_db.header.fields:
    names.append(field.name)
out_csv.writerow(names)

for rec in in_db:
    out_csv.writerow(rec.fieldData)

in_db.close()

 And this:

#!/usr/bin/python

import csv
from dbfpy import dbf
import os
import sys

filename = 'PEDIDOS.DBF'

print "Converting %s to csv" % filename
csv_fn = filename[:-4]+ ".csv"
with open(csv_fn,'wb') as csvfile:
    in_db = dbf.Dbf(filename)
    out_csv = csv.writer(csvfile)
    names = []
    for field in in_db.header.fields:
        names.append(field.name)
    out_csv.writerow(names)
    for rec in in_db:
        out_csv.writerow(rec.fieldData)
    in_db.close()
    print "Done..."


Other file DBF works perfectly, but two dbf files specifically doens't work.

Some idea?

DBF (working)

USUARIO,C,10 TERMINAL,C,10 DATA,D HORA,C,8 PRODUTO,C,6 ESTOQUE,N,3,0 LOTE,C,20 ENTRADA,N,14,4 SAIDA,N,14,4 SALDO,N,14,4 TIPO,N,2,0 OBS,C,254 CRC,N,5,0 AREA,C,3 CLIENTE,N,6,0 SEQ,N,9,0 GRADE_EST SALDO_LOTE,N,14,4 PROCOM,N,14,4 CUSTO_MAX,N,14,4 EN_PROCOM,N,14,4 EN_CST_MAX,N,14,4 ORIGEM,N,2,0 CUSTO_FISC,N,14,4 EN_CST_FIS,N,14,4 DT_ATUALIZ RESERVADO,L






































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































DBF with Problem cols:

FILENAME: PEDPRO.DBF
CODIGO,N,6,0 PRODUTO,C,6 QUANT,N,10,4 PRE_UNI,N,14,4 DESC1,N,7,4 DESC2,N,7,4 PRE_LIQ,N,14,4 COMISSAO,N,6,3 TOT_PRO,N,14,4 TOT_COM,N,14,4 ICMS,N,5,2 TIV,C,2 CUSTO,N,14,4 CUSTO_MED,N,14,4 ICMS_SUB,N,5,2 BASE_SUB,N,6,2 SUB_TRIB,N,14,4 ESTOQUE,N,3,0 IPI,N,5,2 TOT_IPI,N,12,2 CUSTO_MAX,N,14,4 DESCONTO,N,2,0 SAIDAEMB,L REDUTOR,N,7,4 CST,C,3 NCM,C,10 OC,C,15 CFOP,C,15 OBS,M DEPENDENTE,N,3,0 ACRESCIMO,N,6,2 TKT,N,13,0 LOTE,C,20 DATAFAB,D DATAVAL,D VLR_ENTUNI,N,14,4 OBS_INT,M ENTRADA,N,6,0 ITEM_COMP,N,6,0 LV_NSERIE,C,15 LV_NCONTA,N,14,0 COM_TAB,N,3,0 MOEDA,C,5 VAL_MOEDA,N,14,4 SUFRAMA,N,5,2 RED_COM,N,5,2 OPER_PCOM,N,6,3 OPER_TCOM,N,14,4 CONTABIL,N,6,0 VLR_REF,N,14,4 ICMS_REF,N,5,2 TOT_REF,N,14,4 T_REF_ICMS,N,14,4 TERCEIRO,N,4,0 COM_TER,N,6,3 TOT_TER,N,14,4 TABPRE,N,3,0 TABPRE_VLR,N,14,4 TABPRE_VZ,L OS_PECA3,L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_COF,N,15,2 DT_ATUALIZ COD_CEST,C,9 ENQ_IPI,C,3 CONDIFALIQ,L PER_FCP,N,5,2 VLR_FCP,N,14,2 PER_PARTI,N,5,2 VLRPATORI,N,14,2 VLRPATDEST,N,14,2 ICMS_INTER,N,5,2 ITEM_CUPOM,N,3,0


FILENAME:  PEDIDOS.DBF
CODIGO,N,6,0 NAT_OPE,C,15 BLOCO,C,9 DATA,D CLIENTE,N,5,0 COBRANÃA,N,2,0 CONDPGTO,N,1,0 VCTO,C,133 REPRESEN,N,4,0 FATURADO,D NOTA,N,6,0 S╔RIE,N,2,0 TRANSP,N,4,0 TOTAL,N,14,2 ICMS,N,14,2 IPI,N,14,2 ISS,N,14,2 DEVOLUCAO,N,1,0 CANCELADO,L BASE_ICMS,N,14,2 BASE_IPI,N,14,2 BASE_ISS,N,14,2 BASE_SUB,N,14,2 SUB_TRIB,N,14,2 TIPO,N,1,0 TABELA,N,3,0 COD_CONT,C,9 VENDA,N,2,0 ORDSERV,M REDUZIDO,N,6,0 OBSERVACAO,M ENTREGA,D SERVICO,N,14,2 CUPOM,C,6 NUMSERIE,C,20 DATAFAT,D FRETEVALOR,N,14,2 FRETEALIQ,N,5,2 QUANTIDADE,N,14,2 ESPECIE,C,20 MARCA,C,20 DATA_SYS,D PESO_LIQ,N,14,2 PESO_BRU,N,14,2 OC,C,15 MOTORIST,N,6,0 EXP_EDM,L NFCOMPL,L CARGA,N,6,0 HORA,C,8 DEVCLI,L ESTALT,L BAIXAFAT,L IRRF,N,14,2 INSS,N,14,2 OUTRAS,N,14,4 FRETE_PER,N,5,2 FRETE_VAL,N,14,4 FRETE_BC,N,14,4 FRETE_RED,N,7,4 FRETE_ICMS,N,6,2 VLRICMSFRT,N,12,2 GPS_EMIT,L DATA_AGEND,D TROCA,L USUARIO,C,50 ORCAMENTO,N,6,0 ESTORNO,D VLR_ENTREG,N,14,4 QTD_ENTREG,N,6,0 NFCOMPLIPI,L OP_ESTOQUE,N,1,0 OUT_ICMPER,N,5,2 OUT_ICMVAL,N,14,4 CODPED_ORI,N,6,0 FRETE_PAGO,N,14,2 FRETE_CIF,N,14,2 FRETE_FOB,N,14,2 PIS,N,14,2 COFINS,N,14,2 CSSL,N,14,2 DARF_EMIT,L PRAZO_ENT,D FRETE_OBS,M BOLETQUIMI,N,6,0 APT_CLIENT,N,6,0 OPER_TMK,N,4,0 II_IE,N,14,2 PIS_IE,N,14,2 COFINS_IE,N,14,2 DESPADU_IE,N,14,2 SEGURO,N,14,2 CLI_TIPO,N,1,0 OC_PC,C,60 OC_IPC,C,6 CCUSTO,N,6,0 ALTERA_POR,M REP_DIFANO,L REP_DIFAOK,L PEDDEV_ORI,N,6,0 CFOP_JUST,M COM_CLASS,L IMX_SCC,N,6,0 DEMO_DEVOL,D PREV_DEVOL,D DEMO_ENTRA,N,6,0 TERCEIRO,N,4,0 PEDDEV_ARE,C,3 DEMO_AUTOR,C,20 FRETE_PC,N,1,0 SUPERVISOR,N,6,0 SUPERV_COM,N,5,2 EXP_CONTAB,L TERC_RDP,L HORA_FATU RED_COM_SP,N,5,2 ACR_COM_SP,N,6,3 EM_AGUARDO,L CONGAR,L TOTAL_MAN,L TOTAL_PRO,N,14,2 BC_PIS,N,14,2 PER_PIS,N,5,2 BC_COFINS,N,14,2 PER_COFINS,N,5,2 BC_CSSL,N,14,2 PER_CSSL,N,5,2 DED_PCC,L DEDR_PCC,L BC_IRRF,N,14,2 PER_IRRF,N,5,2 DED_IRRF,L DEDR_IRRF,L BC_INSS,N,14,2 PER_INSS,N,5,2 DED_INSS,L DEDR_INSS,L DEDBC_INSS,N,14,2 BC_ISS,N,14,2 PER_ISS,N,5,2 DED_ISS,L DEDR_ISS,L HORA_INC,C,8 NSU,N,10,0 NAO_EMITIR,L NAO_EMIT_M,M PED_FAT,N,6,0 MES_COMPET,N,2,0 ANO_COMPET,N,4,0 DOC_SERV,L NAO_SERTER,N,14,2 NSU_REINM,M SEQ_CONT,N,6,0 PED_MA_ORI,N,6,0 PED_MA_MOT,N,1,0 OF,C,15 DEDBC_IMP1,N,14,2 DAT_SAIDA,D IMPRESSO,D HORA_IMP NFE_SE,L NFE PERC_PARC,N,5,0 CODIF,C,21 ACUMVC_PCC,L NFE_FORMAT,N,1,0 DOC_FISC,C,30 DEV_NOTA,N,6,0 DEV_SERIE,N,2,0 DI,C,15 DEDBC_ISS,N,14,2 OFFLINE_LT ENT_BCST,N,14,2 ENT_ICMSST,N,14,2 DATA_INC,D APROVACAO,D APROV_USU,C,10 NFE_DLL,L NFEM_RPS,N,6,0 NFEM_EXP,L DEDBC_IRRF,N,14,2 BASE_SIMP,N,14,2 ICMS_SIMP,N,5,2 TOTAL_SIMP,N,14,2 CST_SIMP,L PIS_LR,N,14,2 COFINS_LR,N,14,2 PPIS_LR,N,5,2 PCOFINS_LR,N,5,2 PEDORIGPCC,N,6,0 PEDORIGIRR,N,6,0 CLI_PONTOS,N,6,0 CLIPTFATOR,N,14,4 REGISTR_IE,D LOC_DES_IE,C,60 UF_DES_IE,C,2 DAT_DES_IE,D EXPORTA_IE,N,6,0 IOF_IE,N,14,2 RAPEL_PER,N,5,2 RAPEL_VLR,N,14,2 CFOP_FRETE,C,15 ITM_ORI_IE,N,1,0 DAV CRC,N,6,0 DEV_PEDIDO,N,6,0 DEV_COB,N,2,0 DATA_CONF,D HORA_CONF,C,8 USU_CONF,C,10 NFCOMPLPIS,L NFCOMPLCOF,L NFCOMPLCST,C,2 LOC_EMBARQ,C,60 UF_EMBARQ,C,2 OBS_FISC,N,6,0 COPIA_PED,N,6,0 COPIA_AREA,C,3 NFCOMPLOUT,L HR_IMP_DAV ESTORNONFE,L TROCA_CLI,L CLI_OLD,N,5,0 NFSE,L USU_ORC_L,C,30 DH_ORC_L USU_FATU,C,10 PGTO,C,30 COD_PGTO,N,6,0 NFCOMPLST,L EXPEDICAO,N,6,0 DEV_ABACOB,L ORIG_MERC,N,1,0 TIP_PED_AP,C,1 IND_PRESEN,N,2,0 VIATRAN_IE,N,2,0 INTERME_IE,N,2,0 CNPJADI_IE,C,18 UF_ADIC_IE,C,2 AFRMM_IE,N,14,2 MDFE DEMO_ORIG,N,6,0 DT_ATUALIZ MODELO_NFE,C,2 CLI_ENDALT,N,4,0 SERIECOM,N,1,0

Ethan Furman

unread,
Jun 28, 2016, 3:43:06 PM6/28/16
to python...@googlegroups.com
On 06/28/2016 10:10 AM, Vinicius Henrique wrote:
> *
> Hey, guys!
> *
> *I'm converting DBF file to CSV, but the script gives me the following
> error:

[snip]

> I'm trying using this script:*
>
> import csv
> from dbfpy import dbf

Here's your problem: dbfpy is a different package. This user group is
for the package named `dbf`.

Using the `dbf` package, your script would look something like:

import csv
import dbf
import sys

dbf_fn = sys.argv[1]
csv_fn = sys.argv[2]

in_db = dbf.Table(dbf_fn)
in_db.open()
in_db.export(csv_fn)

in_db.close()

--
~Ethan~

Vinicius Henrique

unread,
Jun 28, 2016, 5:42:28 PM6/28/16
to Python dBase
Thanks, Ethan for your response!

I tested but the console give me:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    in_db = dbf.Table('PEDIDOS.DBF')
AttributeError: 'module' object has no attribute 'Table'

)):

This is weird, because, with other dbf file works perfectlly. (sorry for my bad english).
I need convert the DBF to csv to import to mysql.

Adrian Klaver

unread,
Jun 28, 2016, 5:48:27 PM6/28/16
to python...@googlegroups.com
On 06/28/2016 02:42 PM, Vinicius Henrique wrote:
> Thanks, Ethan for your response!
>
> I tested but the console give me:
>
> Traceback (most recent call last):
> File "test.py", line 8, in <module>
> in_db = dbf.Table('PEDIDOS.DBF')
> AttributeError: 'module' object has no attribute 'Table'
>
>
> )):

I guessing you are still using the dbfpy module, not the dbf module
Ethan was talking about

https://pypi.python.org/pypi/dbf/0.96.7
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Python dBase" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to python-dbase...@googlegroups.com
> <mailto:python-dbase...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


--
Adrian Klaver
adrian...@aklaver.com

Adrian Klaver

unread,
Jun 28, 2016, 7:10:34 PM6/28/16
to python...@googlegroups.com
On 06/28/2016 02:42 PM, Vinicius Henrique wrote:
> Thanks, Ethan for your response!
>
> I tested but the console give me:
>
> Traceback (most recent call last):
> File "test.py", line 8, in <module>
> in_db = dbf.Table('PEDIDOS.DBF')
> AttributeError: 'module' object has no attribute 'Table'
>
>
> )):
>
> This is weird, because, with other dbf file works perfectlly. (sorry for
> my bad english).
> I need convert the DBF to csv to import to mysql.

Another option which I just tested is to use LibreOffice Base. It
supports dBase files. You can open a file(as a table) in Base. If you
right click on the table or select rows in the open table you can copy
the data into the LO spreadsheet and save as a CSV from there. You can
also create a query that selects some portion of the dbf file and do the
same thing.

Vinicius Henrique

unread,
Jun 29, 2016, 8:39:04 AM6/29/16
to Python dBase
I use:

pip install dbf

but the pip reports that I already have that module, then I upgrade.

Now the follow error:

\Traceback (most recent call last):
  File "PEDIDOS.py", line 44, in <module>

    for rec in in_db:
  File "C:\Python27\lib\site-packages\dbfpy\dbf.py", line 242, in __getitem__
    return self.RecordClass.fromStream(self, self._fixIndex(index))
  File "C:\Python27\lib\site-packages\dbfpy\record.py", line 121, in fromStream
    return cls.fromString(dbf, cls.rawFromStream(dbf, index), index)
  File "C:\Python27\lib\site-packages\dbfpy\record.py", line 140, in fromString
    [_fd.decodeFromRecord(string) for _fd in dbf.header.fields])
  File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 173, in decodeFromR
ecord
    return self.decodeValue(self.rawFromRecord(record))
  File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 342, in decodeValue

    raise NotImplementedError
NotImplementedError

C:\dbfpy>


)):

I need to automate the process, the libreoffice will not be feasible.

With another DBF I do it easily, are these two specific files that this error is displayed.

Thanks, guys!


Adrian Klaver

unread,
Jun 29, 2016, 9:26:09 AM6/29/16
to python...@googlegroups.com
On 06/29/2016 05:39 AM, Vinicius Henrique wrote:
> I use:
>
> pip install dbf
>
> but the pip reports that I already have that module, then I upgrade.
>
> Now the follow error:
>
> \Traceback (most recent call last):
> File "PEDIDOS.py", line 44, in <module>
> for rec in in_db:
> File "C:\Python27\lib\site-packages\dbfpy\dbf.py", line 242, in
^^^^^^^
You are still using dbfpy. My guess is you are using the code examples
from here:

http://dbfpy.sourceforge.net/

and doing this:

from dbfpy import dbf

You should be doing:

import dbf


> __getitem__
> return self.RecordClass.fromStream(self, self._fixIndex(index))
> File "C:\Python27\lib\site-packages\dbfpy\record.py", line 121, in
> fromStream
> return cls.fromString(dbf, cls.rawFromStream(dbf, index), index)
> File "C:\Python27\lib\site-packages\dbfpy\record.py", line 140, in
> fromString
> [_fd.decodeFromRecord(string) for _fd in dbf.header.fields])
> File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 173, in
> decodeFromR
> ecord
> return self.decodeValue(self.rawFromRecord(record))
> File "C:\Python27\lib\site-packages\dbfpy\fields.py", line 342, in
> decodeValue
>
> raise NotImplementedError
> NotImplementedError
>
> C:\dbfpy>
>
>
>
> )):
>
> I need to automate the process, the libreoffice will not be feasible.
>
> With another DBF I do it easily, are these two specific files that this
> error is displayed.
>
> Thanks, guys!
>
>



--
Adrian Klaver
adrian...@aklaver.com
Reply all
Reply to author
Forward
0 new messages