python - DBF - encoding cp1250 -
i have dbf database encoded in cp1250 , reading database using folowing code:
import csv dbfpy import dbf import os import sys filename = sys.argv[1] if filename.endswith('.dbf'): print "converting %s csv" % filename csv_fn = filename[:-4]+ ".csv" open(csv_fn,'wb') csvfile: in_db = dbf.dbf(filename) out_csv = csv.writer(csvfile) names = [] field in in_db.header.fields: names.append(field.name) #out_csv.writerow(names) rec in in_db: out_csv.writerow(rec.fielddata) in_db.close() print "done..." else: print "filename not end .dbf" problem is, final csv file wrong. encoding of file ansi , characters corrupted. ask you, if can me how read dbf file correctly.
edit 1
i tried different code https://pypi.python.org/pypi/simpledbf/0.2.4, there error.
source 2:
from simpledbf import dbf5 import os import sys dbf = dbf5('test.dbf', codec='cp1250'); dbf.to_csv('junk.csv'); output:
python program2.py traceback (most recent call last): file "program2.py", line 5, in <module> dbf = dbf5('test.dbf', codec='cp1250'); file "d:\programfiles\anaconda\lib\site-packages\simpledbf\simpledbf.py", line 557, in __init__ assert terminator == b'\r' assertionerror
i don't know how solve problem.
try using my dbf library:
import dbf dbf.table('test.dbf') table: dbf.export(table, 'junk.csv')
Comments
Post a Comment