2016-10-14 1 views
-3
나는 다음과 같은 연료 소모량을 호출하고있어

,파이썬에서 index()를 사용하려면 무엇을 가져올 필요가 있습니까?

import clr 
    import System 
    import ServiceDesk 
    import BaseModel 
    class Model(BaseModel.Model): 
     def ExecuteCustomAction(self,args,env): 
     resault = self.account.ExecuteService('service',args,env) 
     res = {} 
     if resault.Count>0: 
      for customaction in resault.Rows: 
       CustomActions = customaction['CustomActions'] 
       if CustomActions !="": 
        Lable = self.find_between(CustomActions, "Lable", "d") 
        CallBack = self.find_between(CustomActions, "CallBack", ";") 
        Action = self.find_between(CustomActions, "Action", "f") 
        res['Lable'] = Lable 
        res['CallBack'] = CallBack 
        res['Action'] = Action 
     return res 

    def find_between(text, first, last ,self): 
     try: 
      start = text.index(first) + len(first) 
      end = text.index(last, start) 
      return text[start:end] 

     except ValueError: 
      return "" 

을하지만이를 실행할 때, 그것은 내가 가져올 필요합니까

객체가 더 속성 '인덱스'

을 가지고 말을?

+2

을 당신이 함수를 호출하는 방법? 해당 코드도 추가하십시오. – JRodDynamite

+0

아무 것도 아니지만 '텍스트'가 무엇이든간에 '인덱스'메소드가 없습니다. 그것은 아마도 당신이 생각하는 것이 아닙니다. – RemcoGerlich

+2

'가져 오기'할 필요가 없습니다. 당신은'index' 메소드를 가진 객체를 전달할 필요가 있습니다. – deceze

답변

2

text의 잘못된 값을 전달하면이 오류가 발생합니다. text은 인덱스 메소드가 작동하는 데 필요한 문자열이어야합니다. 예 : 사용중인 코드에서

>>> def find_between(text, first, last ,self): 
...  try: 
...   start = text.index(first) + len(first) 
...   end = text.index(last, start) 
...   return text[start:end] 
...  except ValueError: 
...   return "" 
... 
>>> find_between("some_string", "s", "t", None) 
'ome_s' 

>>> find_between(123, "s", "t", None) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 3, in find_between 
AttributeError: 'int' object has no attribute 'index' 

>>> find_between(None, "s", "t", None) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 3, in find_between 
AttributeError: 'NoneType' object has no attribute 'index' 

는, 아마도, CustomActions는 객체, 그리고 문자열입니다. 함수에 객체를 전달하면 문자열이 아니므로 오류가 발생합니다. 문자열이 아닌지 확인하기 위해 type(CustomActions)을 사용하여 유형을 확인할 수 있습니다. 또한


, 그래서 당신의 서명이 같은 봤어야, 그 자체가 첫 번째 매개 변수이어야한다주의 :

def find_between(self, text, first, last): 
관련 문제