英文:
How can i save field types and field sizes after working in dbf library in Python?
问题
I work with fox pro dbf files in dbf library in Python. I faced one problem. I have in my dbf file field 'DISTANCE' with field 'type-Double, width - 8, decimal - 3' in VisualFoxPro. After creating table in dbf library in Python, 'DISTANCE' field type is 'B binary' after adding two columns, filling them with data and closing dbf in Python i get 'DISTANCE' with field 'type-Double, width - 8, decimal - 0' in VisualFoxPro. Could you explain why it happens and how can i fix it?
Here is my code:
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()
I tried to create new dbf file and fill it with data from original dbf file but i got the same result as after a.m. code. 'DISTANCE' still was 'type-Double, width - 8, decimal - 0' in VisualFoxPro.
I expect to get 'DISTANCE' with field 'type-Double, width - 8, decimal - 3' after program was run.
英文:
I work with fox pro dbf files in dbf library in Python. I faced one problem. I have in my dbf file field 'DISTANCE' with field 'type-Double, width - 8, decimal - 3' in VisualFoxPro. After creating table in dbf library in Python, 'DISTANCE' field type is 'B binary' after adding two columns, filling them with data and closing dbf in Python i get 'DISTANCE' with field 'type-Double, width - 8, decimal - 0' in VisualFoxPro. Could you explain why it happens and how can i fix it?
Here is my code:
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()
I tried to create new dbf file and fill it with data from original dbf file but i got the same result as after a.m. code. 'DISTANCE' still was 'type-Double, width - 8, decimal - 0' in VisualFoxPro.
I expect to get 'DISTANCE' with field 'type-Double, width - 8, decimal - 3' after program was rus.
答案1
得分: 1
在Visual FoxPro中,Double字段的小数位数仅用于显示,不会影响保存数据的精度。Python中的dbf模块不支持Double字段定义中的小数位数指示,因此当在Python中更改表的结构时,它会自动移除该值,而不会影响表中保存的值。
如果您尝试在Visual FoxPro中执行以下操作,以创建具有距离字段(Double,宽度8,小数位3)的rs_refr表:
CREATE TABLE rs_refr (distance B(3))
INSERT INTO rs_refr (distance) VALUES (1234.123)
? rs_refr.distance && 显示 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),而保存的数据不会受到影响。要检查这一点,测试以下代码:
USE rs_refr
? rs_refr.distance && 显示 1234
? EVALUATE("rs_refr.distance") = 1234 && 显示 .F.
? EVALUATE("rs_refr.distance") = 1234.123 && 显示 .T.
因此,数据未受影响,并且与Python更改结构之前的数据相同。
英文:
The number of decimal places in the Double field in VFP is for display only and has no effect on the precision of the data saved, the dbf module in python does not support that decimal value indication in the Double field definition so when the structure of the table is changed in python it automatically removes that value without affecting the saved value in the table.
If you try the following in Visual FoxPro to create the rs_refr table with the distance field as (Double, width 8, decimal 3):
CREATE TABLE rs_refr (distance B(3))
INSERT INTO rs_refr (distance) VALUES (1234.123)
? rs_refr.distance && Displays 1234.123
USE
And then run your Python program that changes the table structure by adding two fields:
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()
If you go back to the table structure in Visual FoxPro you will find the Distance field type changed to be (Double, width 8, decimal 0) while the saved data is not touched, to check that test the following code:
USE rs_refr
? rs_refr.distance && Displays 1234
? EVALUATE("rs_refr.distance") = 1234 && Displays .F.
? EVALUATE("rs_refr.distance") = 1234.123 && Displays .T.
So the data is not touched and is the same as before the structure was changed by Python.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论