【发布时间】:2020-08-10 13:53:54
【问题描述】:
我有一个如下定义的 yml 架构
"cost_center":{
"type":"number",
"title":"cost center",
"required":[
"cost_center"
]
}
现在在我的 yml 文件中,如果我将成本中心定义为 701,那么它就可以正常工作:
cost_center: 701
但如果我将成本中心定义为 0701,那么它会打印 449。
cost_center: 0701
那么,我的问题是为什么额外的前导零会导致不同的值?
我正在使用下面的,根据实际文件验证我的架构
def validateConfig(configFileName):
with open(configSchemaFile, 'r') as pub_config_schema_file:
pubConfigSchema = json.loads(pub_config_schema_file.read())
with open(configFileName, 'r') as pub_config_file:
pubConfigJson = json.dumps(yaml.load(pub_config_file.read(), Loader=yaml.FullLoader))
pubConfig = json.loads(pubConfigJson)
out = {}
try:
jsonschema.validate(pubConfig, pubConfigSchema)
out['exit_code'] = 0
out['config'] = pubConfig
except jsonschema.exceptions.ValidationError as e:
out['exit_code'] = 1
out['error'] = e.message
print(json.dumps(out))
raise
return json.dumps(out)
response = json.loads(validateConfig(path_to_verify + "config.yml"))
if 'cost_center' in response['config']:
local_cost_center = response['config']['cost_center']
现在,如果成本中心是 0701,这个 local_cost_center 给我 449,如果成本中心是 701(开头没有 0)给我正确的值 701
如何解决这个错误?
【问题讨论】:
-
YML 格式是基于 XML 的,看起来与您所拥有的 YAML 格式完全不同。 YAML 文件的 recommended file extension 自 2006 年以来一直是
.yaml。您还使用 EOL python 版本和尚未升级到 2009 年发布的“新”YAML 标准的解析器。
标签: python python-2.7 yaml