创建列表时的Python缩进错误

2024-06-28 20:28:46 发布

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

我得到了一个缩进错误,我不明白为什么,我定义了两个dict列表,第一个可以,但是第二个(遵循相同格式)抛出了一个缩进错误。在

列出一个(没有问题):

itemData = [{'id': 11, 'model': 'm1', 'serial': 'ser123', 'location': 3, 'distance': 2, 'loc': 3},
            {'id': 12, 'model': 'm1', 'serial': 'ser456', 'location': 3, 'distance': 2, 'loc': 3}]

清单二的第一版:

^{pr2}$

投掷:

Traceback (most recent call last):
  File "job_manager.py", line 1, in <module>
    from NewJM import JobMonitor
  File "C:\Users\Jonathan\Documents\Coding\Python\Logistics Code\NewJM.py", line 56
itemData2 = [{'id': 11, 'model': 'm1', 'serial': 'ser123', 'location': 3, 'distance': 2, 'loc': 3},
                                                                                                  ^
IndentationError: unindent does not match any outer indentation level

清单二第二版:

itemData2 = [
    {'id': 11, 'model': 'm1', 'serial': 'ser123', 'location': 3, 'distance': 2, 'loc': 3},
    {'id': 12, 'model': 'm1', 'serial': 'ser456', 'location': 3, 'distance': 2, 'loc': 3},
    {'id': 13, 'model': 'm2', 'serial': 'ser678', 'location': 5, 'distance': 2, 'loc': 5}
]

投掷:

Traceback (most recent call last):
  File "job_manager.py", line 1, in <module>
    from NewJM import JobMonitor
  File "C:\Users\Jonathan\Documents\Coding\Python\Logistics Code\NewJM.py", line 56
itemData2 = [
            ^
IndentationError: unindent does not match any outer indentation level

我很困惑为什么会发生这种情况,特别是因为第一个版本的列表2是直接复制的,只是添加了另一个dict。有什么想法吗?在


Tags: pyidmodel错误lineseriallocationloc
3条回答

看起来这一行出现了缩进,我会添加一些空白检查,并添加pyflakes作为额外的预防措施。同时,我会检查以确保缩进不会发生在该行之前或之后。在

阅读此帖子:

IndentationError: unindent does not match any outer indentation level

(及以上意见)

让我调查itemData2列表周围的空白

使用ctrl + /命令注释列表,我发现:

    #   itemData2 = [
    #     {'id': 11, 'model': 'm1', 'serial': 'ser123', 'location': 3, 'distance': 2, 'loc': 3},
          # {'id': 12, 'model': 'm1', 'serial': 'ser456', 'location': 3, 'distance': 2, 'loc': 3},
    #     {'id': 13, 'model': 'm2', 'serial': 'ser678', 'location': 5, 'distance': 2, 'loc': 5}
 #   ]

本来应该是这样的:

^{pr2}$

它是混乱的标签和空间,就像前面提到的

看起来你的代码中有空格混合的标签。 从你的问题中复制粘贴你的代码是正确的。在

相关问题 更多 >