python - 在文本文件中搜索。错误:“str”对象不可调用
问题描述
对不起,我在 Python 中很新。尝试使用正则表达式从文本文件中打印包含子字符串“error”的所有行。遇到类型错误,有什么建议吗?
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
解决方案
用作str
变量以在上方某处保存字符串。只需要重命名变量
推荐阅读
- database - 数据库设计:数据库中的两个相关表都引用第三个表。如何保证三者关系一致?
- c++ - 从Meshroom制作的.obj中读取纹理坐标
- reactjs - 确保在过滤方法之前初始化数据
- math - 沿直线均匀分布n个点
- javascript - 如何通过按 ctrl 键或 shift 键在 html 表格中选择多行?
- reactjs - React Native - 编辑平面列表,但平面列表保持不变
- javascript - 在功能组件中添加窗口事件监听器的正确做法
- mongodb - 如何同时执行 $group 和 $lookup 聚合
- sql - 在sql中强制分区
- .htaccess - 在htaccess中从http重定向到https?