2017-11-13 31 views
-1

저는 Python에서 초보자입니다. CMD에서이 명령을 실행하려고하면이 오류가 발생했습니다. . 알려 주시기 바랍니다 :return _compile (pattern, flags) .findall (string)과 관련된 오류 TypeError : 예상되는 문자열 또는 바이트 형 객체

코드 :

import re 
handle=open('regex_sum_41718.txt') 
for line in handle: 
    word=line.split() 
    print(type(word)) 
    y=re.findall('[0-9]+',word) 
print(y) 

오류 :

Traceback (most recent call last): 
    File "Assi_1.py", line 5, in <module> 
     y=re.findall('[0-9]+', word) 
    File "D:\Python\lib\re.py", line 222, in findall 
     return _compile(pattern, flags).findall(string) 
TypeError: expected string or bytes-like object 

덕분에 많이.

답변

1

당신은 line.split() 주위에 다른 루프를 넣어해야합니다

for line in handle: 
    for word in line.split(): 
     print(type(word)) 
     y=re.findall('[0-9]+',word) 

것은 그렇지 않으면 word이 목록입니다 만 문자열을 검색 할 수 있습니다.