2016-07-15 3 views
0

패턴 검색을 시도하고 일치하는 경우 카운터 값에 비트 배열을 설정하십시오. - 먼저 인덱스 항목 자체를가는 당신은 enumerate()의 인수를 혼합파이썬 정규 표현식 오류

if re.search(r'dev_router', this_line) :

2016-07-15T16:27:13: %ERROR: File "/auto/pysw/cel55/python/3.4.1/lib/python3.4/re.py", line 166,

in search 2016-07-15T16:27:13: %-ERROR: return _compile(pattern, flags).search(string)

2016-07-15T16:27:13: %-ERROR: TypeError: expected string or buffer

답변

2

:

runOutput = device[router].execute (cmd) 
      runOutput = output.split('\n') 
      print(runOutput) 
      for this_line,counter in enumerate(runOutput): 
       print(counter) 
       if re.search(r'dev_router', this_line) : 
        #want to use the counter to set something 

다음과 같은 오류를 얻기. 교체 :

for this_line,counter in enumerate(runOutput): 

로 :

for counter, this_line in enumerate(runOutput): 

this_line는 정수이고 re.search() 두 번째 인수로 문자열을 기대 때문에이 경우에 TypeError을 얻고있다. 설명하기 위해 : 그런데

>>> import re 
>>> 
>>> this_line = 0 
>>> re.search(r'dev_router', this_line) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Users/user/.virtualenvs/so/lib/python2.7/re.py", line 146, in search 
    return _compile(pattern, flags).search(string) 
TypeError: expected string or buffer 

PyCharm 같은 현대의 IDE는 정적으로 문제의이 종류를 감지 할 수 있습니다 :

enter image description here

+0

(파이썬 3.5이 스크린 샷에 사용됩니다) 그리고 카운터는 다음과 같이 간단 할 수 있습니다 : before counter'counter = 0', 그리고'counter + = 1'과 일치하면 –