开发者社区> 问答> 正文

Python执行时,re.search报错,求原因及解决方案。 ?报错

执行以下代码时,re.search报错。按理说我代码没啥问题,不知道怎么回事。求大神帮助!

hand=open("regex_sum_42.txt")
for line in hand:
    line=line.rstrip()
    if re.search("Why",line):
        print(line)

执行结果:
Traceback (most recent call last):
File "task1.py", line 4, in
if re.search("Why",line):
NameError: name 're' is not defined

展开
收起
爱吃鱼的程序员 2020-06-23 00:21:14 479 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    在开头添加importre,你没有导入这个模块。

    2020-06-23 00:21:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载