开发者社区 问答 正文

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 541 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

    2020-06-23 00:21:31 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论