본문 바로가기
파이썬

파이썬 TypeError : 개체는 첨자 할 수 없습니다.

by º기록 2020. 11. 5.
반응형

이 오류가 발생했습니다.

  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"]

내가 뭘 잘못하고 있죠?

 

해결 방법

 

d [ "descriptionType"] 을 사용하면 "descriptionType" 키로 d 에 액세스하려고합니다. 하지만 d 는 키가없는 Desk 객체이기 때문에 작동하지 않습니다. 대신 다음 속성을 가져 오십시오.

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

 

참조 페이지 https://stackoverflow.com/questions/36335328

 

 

반응형

댓글