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

查看:365
本文介绍了python语言读写dbf文件,中文编码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

第一次在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编码来打开,这样,所有的中文,由于编码不同,显示的当然就不可能是我想要显示的内容。

不知道我有没有描述清楚。。
这个问题,应该如何处理?

解决方案

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

这篇关于python语言读写dbf文件,中文编码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆