简体   繁体   English

如何调试 python 中的缩进错误

[英]How to debug indentation errors in python

I am trying to write my very first python script.我正在尝试编写我的第一个 python 脚本。 This was working but then after some slight refactoring I have, apparently, broken the indentation.这是可行的,但经过一些轻微的重构后,我显然打破了缩进。 I can not determine what is the problem.我无法确定是什么问题。 The interpretor complains about the following method.解释器抱怨以下方法。 Can someone point it out?有人可以指出吗?

def dataReceived(self, data):
    a = data.split(':')
    print a
    if len(a) > 1:
        command = a[0]
        content = a[1]

        msg = ""
        if command == "iam":
            self.name = content
            msg = self.name + " has joined"

        elif command == "msg":
            msg = self.name + ": " + content
            print msg

The error reads: File "python_server.py", line 17 a = data.split(':') ^ IndentationError: expected an indented block错误显示:文件“python_server.py”,第 17 行 a = data.split(':') ^ IndentationError: expected an indented block

I encountered a similar problem using Sublime Text 2.我在使用 Sublime Text 2 时遇到了类似的问题。

To solve, click on the "Tab Size" at the bottom of the editor, and choose "Convert Indentation to Tabs".要解决,请单击编辑器底部的“Tab Size”,然后选择“Convert Indentation to Tabs”。

You start using a text editor that allows you to show indents, and you become consistent about using spaces instead of tabs, and you enforce that in your editor.你开始使用一个允许你显示缩进的文本编辑器,你开始坚持使用空格而不是制表符,并在你的编辑器中强制执行。

There are a great number of things you can do here:您可以在这里做很多事情:

  1. Use an editor that can show control characters (like vi with set list ).使用可以显示控制字符的编辑器(如vi with set list )。
  2. Use a hex dumper program like od -xcb .使用像od -xcb这样的十六进制转储程序。
  3. Just delete the white space at the start of that line and re-insert it (may want to check the preceding line as well).只需删除该行开头的空格并重新插入它(可能还需要检查前面的行)。

if you're using the "Sublime Text 2" editor, then I found this answer helpful - it details how to turn on whitespace characters and also convert tabs to whitespaces如果您使用的是“Sublime Text 2”编辑器,那么我发现这个答案很有帮助 - 它详细说明了如何打开空白字符以及如何将制表符转换为空白

sublime-text-2-view-whitespace-characters sublime-text-2-view-whitespace-characters

Try Editra - www.editra.org试试 Editra - www.editra.org

Your code looks fine, syntax seems fine...your text editor may be creating your errors.您的代码看起来不错,语法也不错……您的文本编辑器可能会造成您的错误。 Review your file with Editra to see/review indentation levels.使用 Editra 查看您的文件以查看/查看缩进级别。

Editra saved my sanity - I thought I had correct syntax when viewing my script in Text Editors including Notepad++ with python indent plugin. Editra 挽救了我的理智——我认为在文本编辑器(包括带有 python 缩进插件的 Notepad++)中查看我的脚本时我的语法是正确的。 However, when I would run the script, it would throw off indentation errors every time.但是,当我运行脚本时,它每次都会抛出缩进错误。 I finally opened the script up in Editra, and I could see the problem.我终于在 Editra 中打开脚本,我可以看到问题所在。 Notepad++ and other text editors did not show correct indentations/tabs/spaces. Notepad++ 和其他文本编辑器没有显示正确的缩进/制表符/空格。 Editra showed errors eg unexpected spaces, tabs - which I was able to correct. Editra 显示错误,例如意外的空格、制表符——我能够更正这些错误。

Editra will auto-indent your script [and show errors -tabs, spaces -that may not show up in other text editors]. Editra 将自动缩进您的脚本 [并显示错误 - 制表符、空格 - 这可能不会出现在其他文本编辑器中]。

If you have indent errors it will show up as blue underlined segment;如果您有缩进错误,它将显示为带蓝色下划线的段;

If you are writing script [adding/deleting lines] Editra will auto-indent the script.如果您正在编写脚本 [添加/删除行] Editra 将自动缩进脚本。

**I would suggest opening your script and editing it in Editra. **我建议打开您的脚本并在 Editra 中进行编辑。

Hope this helps.希望这可以帮助。 Best of luck.祝你好运。 str8arrow str8箭头

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM