0
可以说我有一个包含这个文件:如何验证值相匹配的数据类型的Python
XXX = Name;
XXX.Value = 15;
XXX.DataType = 'uint8';
YYY = Name;
YYY.Value = -50;
YYY.DataType = 'sint8';
YYY = Name;
ZZZ.Value = 123.123;
ZZZ.DataType = 'float'
XYZ = Name;
XYZ.Value = true;
XYZ.DataType = 'boolean'
现在我有一个JSON文件,该文件是这样的:
{
"XXX": -20,
"XYZ": 50
}
我如何验证是否为正确的名称给出了正确的值/布尔值?例如,由于uint8,XXX应该只能允许正数,并且不能为负数。尽管XYZ只能使用布尔值,而不能使用其他值或字符串。
到目前为止,我的代码检查json文件中的“名称”是否存在于“.txt”文件中,然后检查DataType是什么。
jdata = json.loads(open("test.json").read())
for root, dirs, files in os.walk(directory):
for key, value in jdata.iteritems():
for file in files:
with open(os.path.join(root, file)) as fle:
content = fle.read()
if file.endswith('.txt') and key in content:
re.search(regexDataType(key), content)
print "Name", key, "found in ", file
tt = re.search(regexDataType(key), content)
print tt.group(2) #Here I get the datatype for the specific "name" from Json file.
#How do I move on from here? How do I validate the value from json file is the correct one?
else:
print "Name", key, "not found in file", file