【问题标题】:Indentation error in VS Code for Python scriptVS Code for Python 脚本中的缩进错误
【发布时间】:2020-06-01 09:00:46
【问题描述】:

我正在尝试在 VS Code 中运行以下脚本,但是我不断收到缩进错误,这有点不言自明,但我无法找到错误?有没有办法在 VS Code 中自动格式化?

import logging
import uuid
import json

import azure.functions as func


def main(msg: func.QueueMessage, message: func.Out[func.Document]) -> None:

    logging.info('Python queue trigger function processed a queue item: %s', msg_body)
    data = json.dumps({
        'id': msg.id,
        'body': msg.get_body().decode('utf-8'),
        'expiration_time': (msg.expiration_time.isoformat()
                            if msg.expiration_time else None),
        'insertion_time': (msg.insertion_time.isoformat()
                           if msg.insertion_time else None),
        'time_next_visible': (msg.time_next_visible.isoformat()
                             if msg.time_next_visible else None),
        'pop_receipt': msg.pop_receipt,
        'dequeue_count': msg.dequeue_count
    })
    message.set(func.Document.from_json(json.dumps(data)))

我运行脚本时的错误信息:

[Running] python -u "c:\Users\artem\Desktop\function\inspariqueuestore\__init__.py"
  File "c:\Users\artem\Desktop\function\inspariqueuestore\__init__.py", line 12
    data = json.dumps({
    ^
IndentationError: unexpected indent

[Done] exited with code=1 in 0.093 seconds

更新

我显然混合了制表符和空格。问题已解决。

【问题讨论】:

  • 你是否混合了制表符和空格?
  • 我不能完全否认我可能无意中混合了它。我会尝试用空格重新格式化它。谢谢
  • @KlausD。重新格式化成功了。谢谢!

标签: python visual-studio-code


【解决方案1】:

我没有在您的文件中看到任何缩进错误。

是的,您可以尝试使用 VS Code 格式化您的文件。 有一个名为editor.action.formatDocument 的快捷方式。 您可以在设置中找到您的设置的确切键。

在我的例子中是shift + option (alt) + F

【讨论】:

  • 我尝试使用 autopep8 进行格式化,但没有改变缩进。当我尝试调试该函数时,由于意外缩进,它仍然失败。
  • @Artem 谢谢。您粘贴到 SO 的代码似乎没问题。您可以尝试删除data 变量之前的缩进然后将其放回原处吗?也许你混合了制表符和空格?
  • 我混合了制表符和空格 - 只是通过搜索和替换重新格式化了整个内容。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多