0

错误:回溯(最近一次调用最后一次):文件“c:\Users\Pranjal\Desktop\tstp\zen_scraper.py”,第 5 行,用文字表示 = re.findall("$y",file) 文件“C:\ Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2288.0_x64__qbz5n2kfra8p0\lib\re.py",第 241 行,在 findall 中 return _compile(pattern, flags).findall(string) TypeError: expected string or bytes-like object PS C:\用户\Pranjal\桌面\tstp>

import re

file = open("zen.txt",'r')

words = re.findall("$y",file)
print(words)
4

1 回答 1

0

您已打开文件,但尚未获得其内容。此外,re这里没有必要,str.endswith()是您所需要的。

with open("zen.txt",'r') as f:
    for line in f:
        for word in line.split():
            if word.endswith('y'):
                print(word)
于 2021-03-20T14:12:55.603 回答