TypeError:Flask中的字符串索引必须是整数

时间:2018-09-03 05:34:48

标签: python flask error-handling casting

我正在使用python词典执行我的任务之一,并且此错误提示与以下提及的语句有关。

def flat(self, data, no_of_attrs):
    if isinstance(data, list):
        if len(data) == 1:
            self.flat(data[0], no_of_attrs)
        elif len(data) > 1:
            for j in data:
                self.flat(j, no_of_attrs)
        else:
            return
    #if dictionary
    else:
        if len(data)==1:
            if data['parent_id']==0 or 1:
                self.flat(self, data['children_data'], no_of_attrs)
            else:
                n_item = item(data['id'], data['parent_id'], data['name'], data['is_active'], data['position'], data['product_count'], data['path'], data['name_ar'])

发生的错误与条件if data['parent_id']==0 or 1:有关。

有人可以看到这个问题,并帮助我解决这个问题。

[{"id": 6, "parent_id": 5, "name": "Boots", "enabled": true, "position": 1,  "children_data": [],  "path": "/women/shoes/boots"}, {"id": 1523, "parent_id": 5, "name": "Flats", "enabled": true, "position": 2, "children_data": [], "path": "/women/shoes/flats"}, {"id": 16, "parent_id": 5, "name": "Ballerinas", "enabled": true,"count": 12505, "children_data": [], "path": "/women/shoes/ballerinas"}]

enter image description here

PS:-数据是字典

0 个答案:

没有答案