"Pyxb十进制数据类型丢失零"

2024-06-16 14:40:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我注意到pyxbdecimal数据类型在呈现为XML时不保留尾随的零。罪魁祸首是对normalize()函数下一行XsdLiteralbinding/datatypes.py的第159行中的normalize()的调用:

(sign, digits, exponent) = value.normalize().as_tuple()

(其中value是Python的decimal的实例)。这对我来说有点问题,因为我尝试与之交互的web服务需要X.000形式的版本号,pyxb正在将其截断为X.0。你知道吗

这是预期的行为吗?或者某些标准要求的?其他XML模式生成库也这样做吗?我现在的解决方案是改用string,但是如果代码不破坏任何东西,那么代码很容易更改。你知道吗


Tags: 函数代码pyvalueasxmlbinding数据类型
2条回答

我没有使用pyxb的经验,但我的猜测是,一般来说,人们希望XML尽可能紧凑以保留内存,因此小数会截断以保留字节。你知道吗

这似乎不是一个正常的情况下使用小数点。我认为应该使用十进制来存储数学数值,因此截断总是可能的。因为你的案例很特殊,所以这个模块可能不是为你想做的事情而设计的。你知道吗

官方的PyXB响应是here,但是来自the description of the canonical representation of an xs:decimal value

Leading and trailing zeroes are prohibited subject to the following:
there must be at least one digit to the right and to the left of the
decimal point which may be a zero.

同样在十进制本身的描述中:

Precision is not reflected in this value space; the number 2.0 is not
distinct from the number 2.00.

服务提供商要求提供尾随零,因此不符合要求。你知道吗

相关问题 更多 >