使用python脚本生成的dbf由OpenOffice Calc解释为cp850而不是cp1252

使用python脚本生成的dbf由OpenOffice Calc解释为cp850而不是cp1252,python,python-2.7,dbf,Python,Python 2.7,Dbf,此脚本生成了一个在dbf查看器中看起来不错的字符集,但OpenOffice Calc默认为西欧(DOS/OS2-850国际)字符集。打开时,非ASCII字符不正确。当使用西欧(Windows-1252/WinLatin 1)字符集时,它看起来很好。MS Access 2000的解释类似于DOS 850。我是否缺少某个设置或参数 import sys, os.path, string import dbf as dbfpp print 'stdin:', sys.stdin.encoding,

此脚本生成了一个在dbf查看器中看起来不错的字符集,但OpenOffice Calc默认为西欧(DOS/OS2-850国际)字符集。打开时,非ASCII字符不正确。当使用西欧(Windows-1252/WinLatin 1)字符集时,它看起来很好。MS Access 2000的解释类似于DOS 850。我是否缺少某个设置或参数

import sys, os.path, string 
import dbf as dbfpp
print 'stdin:', sys.stdin.encoding, 'stdout:', sys.stdout.encoding # cp437 both if run in win cmd.exe, None, utf-8 in pywin

def main(): 
    flddef = 'SITE_CODE N(19,6);HW_CODE C(13);PROGRAM C(10);SITE_NAME C(85);START_DATE D;DESCRIPT M;ACRES N(19,6);'
    dbffile = 'X:/Import/ny/esrd/esrd2015test_ny/outdbf/test.dbf'
    if os.path.isfile(dbffile):
        os.remove(dbffile)
    dbfmake = dbfpp.Table(
        dbffile, 
        flddef, 
        codepage='cp1252',
        on_disk=True,
        )
    dbfmake.open()
    print dbfmake.codepage # cp1252 (Windows ANSI)
    descvalinit = 'The site was on Cayuga Lake ¼ mile from shore (these non-gremlins): µ©®æ§. A gremlin phrase:“only perch”.'
    print 'descvalinit in list:', [descvalinit] # ['The site was on Cayuga Lake \xbc mile from shore (these non-gremlins): \xb5\xa9\xae\xe6\xa7. A gremlin phrase:\x93only perch\x94.']
    descval = unicode(descvalinit, '1252')
    print 'descval in list:', [descval] # [u'The site was on Cayuga Lake \xbc mile from shore (these non-gremlins): \xb5\xa9\xae\xe6\xa7. A gremlin phrase:\u201conly perch\u201d.']
    datum = (33567.000000, '100000B ', 'HW', 'Fishing spot', None, descval, 18)
    print 'datum:', datum # (33567.0, '100000B ', 'HW', 'Fishing spot', None, u'The site was on Cayuga Lake \xbc mile from shore (these non-gremlins): \xb5\xa9\xae\xe6\xa7. A gremlin phrase:\u201conly perch\u201d.', 18)
    dbfmake.append(datum)
    dbfmake.close()

if __name__ == "__main__":
    main()

在Windows 7 x64上使用python 2.7.3 32位时,dbf文件本身包含的元字段中指定了编码。编写良好的程序应该查看这些信息以确定编码

但是,在中,有些程序在单独的
.cpg
.cst
文件中查找以获取编码

我发现(但无法重新找到)2008年的一篇论坛帖子,讨论
Calc
未能正确解码
dbf
内容


因此,在这一点上,我将尝试使用单行
cp1252
创建一个
test.cpg
文件,看看它是否有效(如果无效,则尝试
test.cst

dbf
文件在
dbf
文件本身包含的元字段中指定了编码。编写良好的程序应该查看这些信息以确定编码

但是,在中,有些程序在单独的
.cpg
.cst
文件中查找以获取编码

我发现(但无法重新找到)2008年的一篇论坛帖子,讨论
Calc
未能正确解码
dbf
内容


因此,在这一点上,我将尝试使用单行
cp1252
创建一个
test.cpg
文件,看看它是否有效(如果无效,则尝试
test.cst

这两个文件都没有更改Calc或Access的行为。顺便说一句,我提到Calc是因为我认为它显示了一个编码问题。我真的很关心将数据导入MS Access。您可能希望转换为csv并导入该数据;数据以csv格式开始,通过编程进行轻微编辑并加载到postgres表中,因此我可能可以从那里将其导入Access。希望有一个传统GIS程序和Access都可以读取的dbf。这两个文件都没有改变Calc或Access的行为。顺便说一句,我提到Calc是因为我认为它显示了一个编码问题。我真的很关心将数据导入MS Access。您可能希望转换为csv并导入该数据;数据以csv格式开始,通过编程进行轻微编辑并加载到postgres表中,因此我可能可以从那里将其导入Access。希望有一个dbf,一个遗留的GIS程序和Access都可以读取。