TypeError:object不可订阅

时间:2016-03-31 13:47:54

标签: python typeerror

我收到了这个错误

  File "/class.py", line 246, in __init__
if d and self.rf == 2 and d["descriptionType"] in ["900000000000003001"] and d["conceptId"] in konZer.zerrenda:
TypeError: 'Desk' object is not subscriptable

我创建了这个对象

class Desk:
     descriptionId = ""
     descriptionStatus = ""
     conceptId = ""
     term = ""

我在另一个班级打电话给她

class DescriptionList():

     def deskJ(self,line):
         er = line.strip().split('\t')
         desc = Desk()
         if er[1] == "0":
            desc.descriptionId = er[0]
            desc.descriptionStatus = er[1]
            desc.conceptId = er[2]
            desc.term = er[3]
         return description

然后我在 init 调用了函数“deskJ”,我在这部分得到错误(我删除了函数的某些部分):

def __init__(self,fitx,konZer,lanZer={}):
        with codecs.open(fitx,encoding='utf-8') as fitx:
            lines = fitx.read().split('\n')[1:-1]
            for line in lines:
                d = self.deskJ(line)
                if d and self.rf == 2 and d["descriptionType"] in ["900000000000003001"] and d["conceptId"] in konZer.zerrenda:
                    c = konZer.zerrenda[d["conceptId"]]
                    c["fullySpecifiedName"] = d["term"]

我做错了什么?

1 个答案:

答案 0 :(得分:1)

使用d["descriptionType"]正在尝试使用密钥d访问"descriptionType"。但这不起作用,因为d是一个没有键的Desk对象。相反,获取属性:

if d and self.rf == 2 and d.descriptionType in ["900000000000003001"] and d.conceptId in konZer.zerrenda:
相关问题