在文本文件中搜索。错误:“ str”对象不可调用

时间:2019-07-22 16:52:00

标签: python string text typeerror

对不起,我在Python中很陌生。尝试使用正则表达式从文本文件中打印包含子字符串“错误”的所有行。 遇到类型错误,请问任何建议吗?

import re
errors = []
linenum = 0
pattern = re.compile("error", re.IGNORECASE)  # Compile a case-insensitive regex
with open ('Setup.log', 'rt') as myfile:    
    for line in myfile:
        linenum += 1
        if pattern.search(line) != None:      # If a match is found 
            errors.append((linenum, line.rstrip('\n')))
for err in errors:                            # Iterate over the list of tuples
    print("Line " + str(err[0]) + ": " + err[1]) 
TypeError                                 Traceback (most recent call last)
<ipython-input-72-efb36dfad1d6> in <module>()
      9             errors.append((linenum, line.rstrip('\n')))
     10 for err in errors:                            # Iterate over the list of tuples
---> 11     print("Line " + str(err[0]) + ": " + err[1])

TypeError: 'str' object is not callable

1 个答案:

答案 0 :(得分:0)

使用str作为变量将字符串保存在上方。 只需重命名变量