在Python中使用dbf库后如何保存字段类型和字段大小?

问题描述 投票:0回答:1

我在Python的dbf库中使用fox pro dbf文件。我遇到一个问题。我在 VisualFoxPro 中的 dbf 文件字段“DISTANCE”和字段“type-Double,width - 8,decimal - 3”中。在Python中的dbf库中创建表后,添加两列、用数据填充它们并在Python中关闭dbf后,“DISTANCE”字段类型为“B二进制”,我得到“DISTANCE”,字段为“type-Double,宽度 - 8,十进制” - VisualFoxPro 中的 0'。您能解释一下为什么会发生这种情况以及如何解决它吗?

这是我的代码:

import dbf

rs_refr = dbf.Table('rs_refr.dbf', codepage = 'cp1252', ignore_memos = True)
rs_refr.open(dbf.READ_WRITE)

rs_refr.add_fields('UT_WT N(5,2)')
rs_refr.add_fields('WT_UT N(5,2)')

for record in rs_refr:
    wt = record['WT']
    with record as r:
        r['UT_WT'] = wt
        r['WT_UT'] = wt

db.close()

我尝试创建新的 dbf 文件并用原始 dbf 文件中的数据填充它,但得到的结果与上午代码后的结果相同。在 VisualFoxPro 中,“DISTANCE”仍然是“type-Double,宽度 - 8,小数 - 0”。

我希望在程序被使用后得到带有“type-Double,width - 8,decimal - 3”字段的“DISTANCE”。

python foxpro dbf
1个回答
0
投票

VFP中Double字段的小数位数仅用于显示,对保存数据的精度没有影响,python中的dbf模块不支持Double字段定义中的小数位数指示,因此当结构为在 python 中更改表后,它会自动删除该值,而不会影响表中保存的值。 如果您在 Visual FoxPro 中尝试以下操作来创建距离字段为(双精度,宽度 8,小数 3)的 rs_refr 表:

CREATE TABLE rs_refr (distance B(3))
INSERT INTO rs_refr (distance) VALUES (1234.123)
? rs_refr.distance && Displays 1234.123
USE

然后运行 Python 程序,通过添加两个字段来更改表结构:

import dbf

rs_refr = dbf.Table('rs_refr.dbf', codepage = 'cp1252', ignore_memos = True)
rs_refr.open(dbf.READ_WRITE)

rs_refr.add_fields('UT_WT N(5,2)')
rs_refr.add_fields('WT_UT N(5,2)')

rs_refr.close()

如果你回到 Visual FoxPro 中的表结构,你会发现 Distance 字段类型变成了(Double,宽度 8,小数 0),而保存的数据没有被触及,检查测试以下代码: 使用 rs_refr ? rs_refr.distance && 显示 1234 ? EVALUATE("rs_refr.distance") = 1234 && 显示 .F。 ? EVALUATE("rs_refr.distance") = 1234.123 && 显示.T.

因此数据没有被触及,并且与Python更改结构之前相同。

© www.soinside.com 2019 - 2024. All rights reserved.