我应该如何处理这个意外的缩进?

2024-03-28 17:16:29 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图运行这段代码时,它给了我一条错误消息line21 UnexpectedIndent。我该怎么办?谢谢。你知道吗

for filepath in matches:
    with open (filepath,'rt') as mytext:
         mytext=mytext.read()

         print re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext)  #This line just aims to diagnose the problem.
         matchOrg=re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext)[0]
         capturedorgs.append(matchOrg)

当我摆脱print re.findall(r'NSF\s+Org\s+\:\s+(\w+)',mytext)时,错误消息是MatchOrg... list out of range。你知道吗

去开会。将在4p后检查所有回复


Tags: 代码inorgre消息for错误print
2条回答

mytext=...用制表符缩进,其余行用空格缩进。使用制表符或空格,但不要混合使用。鼓励使用空格而不是制表符。你知道吗

在文本编辑器中,使用该选项可显示所有字符。确保缩进用空格或制表符表示,而不是两者的混合。你知道吗

当您从其他源复制代码片段时,源代码和其他源代码的缩进可能不同,从而导致python解释器抛出意外的缩进错误,如下所示

enter image description here

相关问题 更多 >