Python 正确计算了两个条件,但执行了错误的块

Python 正确计算了两个条件,但执行了错误的块,python,python-3.x,django-rest-framework,Python,Python 3.x,Django Rest Framework,数据或多或少包含以下内容: [ {"code": "F1", "description": "Item 1", "quantity": 2, "group": "G1"}, {"code": "C4", "description": "Item 4", "quantity": 6, "group": "GS"}, {"code": "D2", "description": "Item 2", "quantity": 6, "group": "G1"} ] 此外,我希望在

数据
或多或少包含以下内容:

[
    {"code": "F1", "description": "Item 1", "quantity": 2, "group": "G1"},
    {"code": "C4", "description": "Item 4", "quantity": 6, "group": "GS"},
    {"code": "D2", "description": "Item 2", "quantity": 6, "group": "G1"}
]
此外,我希望在上面的JSON中有一些键:

keys=['code','description','quantity','group\u code','group\u name']
因此,
数据
被加载,操作开始。执行此循环:

数据中的d的
:
对于d中的i:
打印('循环数据:',d',键:',i)
打印('str(i)在键中:',str(i)在键中)
如果isinstance(d[i],int):
打印('检测到整数'*3)
尝试:
如果键中有d[i]和str(i):
通过
除KeyError外:
引发序列化程序。ValidationError(“错误!参数\”“+str(i)
+“\”在中不正确!”)
其他:
打印('未检测到整型')
尝试:
如果len(d[i])>0且键中有str(i):
通过
除KeyError外:
引发序列化程序。ValidationError(“错误!参数\”“+str(i)
+“\”不存在于中!”)
运行后,我预计会遇到错误,因为
键不在
数组中,并且键中的条件
str(I)应计算为未通过,因为它是
,但没有引发异常:

looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: code
str(i) in keys: True
not int detected
looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: description
str(i) in keys: True
not int detected
looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: quantity
str(i) in keys: True
int detected int detected int detected
looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: group
str(i) in keys: False ########### <-- no exception!
not int detected
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: code
str(i) in keys: True
not int detected
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: description
str(i) in keys: True
not int detected
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: quantity
str(i) in keys: True
int detected int detected int detected
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: group
str(i) in keys: False ########### <-- no exception!
not int detected
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: code
str(i) in keys: True
not int detected
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: description
str(i) in keys: True
not int detected
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: quantity
str(i) in keys: True
int detected int detected int detected
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: group
str(i) in keys: False ########### <-- no exception!
not int detected
循环数据:{'code':'F1','description':'Item 1','quantity':2','group':'G1'},关键字:code
键中的str(i):真
未检测到int
循环数据:{'code':'F1','description':'Item 1','quantity':2',group':'G1'},关键字:description
键中的str(i):真
未检测到int
循环数据:{'code':'F1','description':'Item 1','quantity':2',group':'G1'},关键字:quantity
键中的str(i):真
检测到整数检测到整数检测到整数检测到整数
循环数据:{'code':'F1','description':'Item 1','quantity':2',group':'G1'},关键字:group

键中的str(i):False我相信这就是你的本意:

for d in data:
    for i in d:
        print('looped data:', d, ', key:', i)
        print('str(i) in keys:', str(i) in keys)
        if isinstance(d[i], int):
            print('int detected ' * 3)
            if d[i] and str(i) in keys:
                pass
            else: 
                raise serializers.ValidationError("Error! Parameter \"" + str(i)
                    + "\" isn\'t correct in <<" + str(d) + ">>!")
        else:
            print('not int detected')
            if len(d[i]) > 0 and str(i) in keys:
                pass
            else:
                raise serializers.ValidationError("Error! Parameter \"" + str(i)
                    + "\" doesn\'t exist in <<" + str(d) + ">>!")

为了帮助您更好地理解这个问题,我同时打印了
d[I]
[I]
。 输入:


在您描述为
errors
的情况下,满足了if的条件,并且仅遵循条件的
True
值和
pass

如果键中的len(d[i])>0和str(i):
没有引发任何异常,因为
d[i]
有效。因此,您没有看到异常。你的意思是如果len(d[i])>0和str(i)在键中:。。。else:raise ValidationError
?一个
if
语句不会仅仅因为条件为false而引发异常。
for d in data:
    for i, item in d.items():
        # instead of d[i], just use item directly
data = [{"code": "F1", "description": "Item 1", "quantity": 2, "group": "G1"},
    {"code": "C4", "description": "Item 4", "quantity": 6, "group": "GS"},
    {"code": "D2", "description": "Item 2", "quantity": 6, "group": "G1"}
]
keys = ['code', 'description', 'quantity', 'group_code', 'group_name']
for d in data:
    for i in d:
        print('looped data:', d, ', key:', i)
        print('str(i) in keys:', str(i) in keys)
        if isinstance(d[i], int):
            print('int detected ' * 3)
            try:
                if d[i] and str(i) in keys:
                    pass
            except KeyError:
                raise serializers.ValidationError("Error! Parameter \"" + str(i)
                                       + "\" isn\'t correct in <<" + str(d) + ">>!")
        else:
            print('not int detected')
            print(d[i])
            print(i)
            try:
                if len(d[i]) > 0 and str(i) in keys:
                    pass
            except KeyError:
                raise serializers.ValidationError("Error! Parameter \"" + str(i)
                                       + "\" doesn\'t exist in <<" + str(d) + ">>!")
looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: code
str(i) in keys: True
not int detected
F1
code
looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: description
str(i) in keys: True
not int detected
Item 1
description
looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: quantity
str(i) in keys: True
int detected int detected int detected
looped data: {'code': 'F1', 'description': 'Item 1', 'quantity': 2, 'group': 'G1'} , key: group
str(i) in keys: False
not int detected
G1
group
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: code
str(i) in keys: True
not int detected
C4
code
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: description
str(i) in keys: True
not int detected
Item 4
description
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: quantity
str(i) in keys: True
int detected int detected int detected
looped data: {'code': 'C4', 'description': 'Item 4', 'quantity': 6, 'group': 'GS'} , key: group
str(i) in keys: False
not int detected
GS
group
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: code
str(i) in keys: True
not int detected
D2
code
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: description
str(i) in keys: True
not int detected
Item 2
description
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: quantity
str(i) in keys: True
int detected int detected int detected
looped data: {'code': 'D2', 'description': 'Item 2', 'quantity': 6, 'group': 'G1'} , key: group
str(i) in keys: False
not int detected
G1
group