【问题标题】:error- IndentationError: unindent does not match any outer indentation level错误- IndentationError: unindent 不匹配任何外部缩进级别
【发布时间】:2020-05-24 03:08:50
【问题描述】:

我是 Python 的初学者。我正在尝试运行以下代码来替换 .txt 注释文件中的一些标签。

import os

for txt_in in os.listdir(r"uncorrected-YOLO_darknet"):
    with open(txt_in) as infile:
        for line in infile:
            word=line.split(" ")[0]
            if word="6":
                word.replace('6', '5')
            elif word="9":
                word.replace('9', '6')
            elif word="10":
                word.replace('10', '7')
            elif word="11":
                word.replace('11', '8')
            else:
                continue
            break

但我收到以下错误:

File "<tokenize>", line 7
    if word="6":
    ^
IndentationError: unindent does not match any outer indentation level

我试图找到缩进错误,但对我来说看起来没问题。请帮忙。 tnx!

【问题讨论】:

  • 你有混合制表符和空格。找到编辑器的“将制表符转换为空格”按钮并点击它,将缩进设置为空格,并检查是否再次出现。
  • tnx @ user2357112 支持莫妮卡。它有帮助

标签: python error-handling


【解决方案1】:

if/elif 采用返回 boolean 值的语句。

== 就是这样一种运算符,它检查是否相等并返回 true/false

您的代码中有=,它是一个赋值运算符,它不检查是否相等。

像这样替换所有if/elif 语句:

if word == "6":

【讨论】:

    【解决方案2】:

    您使用了错误的 If 和 elif 条件句法
    试试下面的代码

            if word=="6":
                word.replace('6', '5')
            elif word=="9":
                word.replace('9', '6')
            elif word=="10":
                word.replace('10', '7')
            elif word=="11":
                word.replace('11', '8')
            else:
                continue
            break
    

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多