为了账号安全,请及时绑定邮箱和手机立即绑定

python语言读写dbf文件,中文编码的问题

python语言读写dbf文件,中文编码的问题

慕标琳琳 2019-02-23 23:16:08
第一次在segmentfault发帖,也是没办法,才希望能够得到帮助。问题也许很低端,但是,我尝试了一个礼拜了,都没有找到好的解决方法。。。请各位大神帮帮忙 问题描述 希望做到用python来输出一份dbf格式的文件。于是乎,我百度到了这一份python2的代码,可以工作 #!/usr/bin/python # -*- coding: utf-8 -*- import struct import datetime import decimal import itertools def dbfwriter(f, fieldnames, fieldspecs, records): """ Return a string suitable for writing directly to a binary dbf file. File f should be open for writing in a binary mode. Fieldnames should be no longer than ten characters and not include \x00. Fieldspecs are in the form (type, size, deci) where type is one of: C for ascii character data M for ascii character memo data (real memo fields not supported) D for datetime objects N for ints or decimal objects L for logical values 'T', 'F', or '?' size is the field width deci is the number of decimal places in the provided decimal object Records can be an iterable over the records (sequences of field values). """ # header info ver = 3 now = datetime.datetime.now() yr, mon, day = now.year - 1900, now.month, now.day numrec = len(records) numfields = len(fieldspecs) lenheader = numfields * 32 + 33 lenrecord = sum(field[1] for field in fieldspecs) + 1 hdr = struct.pack('<BBBBLHH20x', ver, yr, mon, day, numrec, lenheader, lenrecord) f.write(hdr) # field specs for name, (typ, size, deci) in itertools.izip(fieldnames, fieldspecs): name = name.ljust(11, '\x00') fld = struct.pack('<11sc4xBB14x', name, typ, size, deci) f.write(fld) # terminator f.write('\r') # records for record in records: f.write(' ') # deletion flag for (typ, size, deci), value in itertools.izip(fieldspecs, record): if typ == "N": value = str(value).rjust(size, ' ') elif typ == 'D': value = value.strftime('%Y%m%d') elif typ == 'L': value = str(value)[0].upper() else: value = str(value)[:size].ljust(size, ' ') assert len(value) == size f.write(value) # End of file f.write('\x1A') # ------------------------------------------------------- # Example calls if __name__ == '__main__': f = open('writec.dbf', 'wb') fieldnames = ['姓名', '年龄'] fieldspecs = [("C", 10, 0), ("N", 4, 0)] records = [["安超", 26], ["ac", 30]] dbfwriter(f, fieldnames, fieldspecs, records) f.close() 那么问题来了。。。这样写出的代码,保存的dbf文件中的字段名和数据中的中文,是utf-8编码的,但是,所有的dbf阅读器,打开dbf文件,都必须按照ascii编码来打开,这样,所有的中文,由于编码不同,显示的当然就不可能是我想要显示的内容。 不知道我有没有描述清楚。。这个问题,应该如何处理?
查看完整描述

1 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

你没有明确你的问题,你python代码输出的是utf-8编码格式的内容,但你的dbf阅读器是按照ascii来解析的,你是想让输出改成ascii还是解析的时候用utf-8?

查看完整回答
反对 回复 2019-03-04
  • 1 回答
  • 0 关注
  • 1016 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信