本文介绍了DBF-编码cp1250的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有使用cp1250编码的dbf数据库,并且正在使用以下代码读取此数据库:
I have dbf database encoded in cp1250 and I am reading this database using folowing code:
import csv
from dbfpy import dbf
import os
import sys
filename = sys.argv[1]
if filename.endswith('.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..."
else:
print "Filename does not end with .dbf"
问题是,最终的csv文件错误.该文件的编码为ANSI,并且某些字符已损坏.我想问你,是否可以帮助我如何正确读取dbf文件.
Problem is, that final csv file is wrong. Encoding of the file is ANSI and some characters are corrupted. I would like to ask you, if you can help me how to read dbf file correctly.
编辑1
我尝试了不同于 https://pypi.python.org/pypi/simpledbf的代码/0.2.4 ,有一些错误.
I tried different code from https://pypi.python.org/pypi/simpledbf/0.2.4, there is some error.
来源2:
from simpledbf import Dbf5
import os
import sys
dbf = Dbf5('test.dbf', codec='cp1250');
dbf.to_csv('junk.csv');
输出:
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
AssertionError
我真的不知道如何解决这个问题.
I really don't know how to solve this problem.
推荐答案
尝试使用我的dbf库:
import dbf
with dbf.Table('test.dbf') as table:
dbf.export(table, 'junk.csv')
这篇关于DBF-编码cp1250的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!