对不起,我在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
答案 0 :(得分:0)
使用str
作为变量将字符串保存在上方。
只需重命名变量