Franco
Franco

Reputation: 11

Errors concerning return _compile(pattern, flags).findall(string) TypeError: expected string or bytes-like object

I am a beginner at Python, and when I try to execute this command on CMD, I was faced with this error. Please advise:

Code:

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)

Error:

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

Thanks a lot.

Upvotes: 0

Views: 14784

Answers (1)

Jan
Jan

Reputation: 43169

You need to put another loop around line.split():

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

Otherwise word is a list and only strings can be searched.

Upvotes: 1

Related Questions