代码处理突发缩进错误如何在Python
#!/usr/bin/python
import sys
import psycopg2
import psycopg2.extras
def init_pg94_from_sql_file(filename, connection):
file = open(filename, 'r')
sql = s = " ".join(file.readlines())
print "Start executing: " + filename + " at " + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) + "\n" + sql
cursor = connection.cursor()
cursor.execute(sql)
connection.commit()
cursor.close()
,但我得到
File "9.7.2015.py", line 14
cursor.close()
^
IndentationError: unexpected indent
这是奇怪的,因为我似乎没有任何压痕问题。
如何处理这种意想不到的缩进挑战?
仔细检查您的缩进!你可能有一个流浪的空间或一个流浪的选项卡导致问题! (我建议你重新缩进你的代码,单独使用空格。) – NightShadeQueen
另外,一些编辑器(vim就是其中之一)能够将制表符强制为空格。如果你的编辑有这个能力,我建议打开它。 – NightShadeQueen
您可能会混合使用空格和制表符。为了解决这个问题,请在IDLE shell中打开文件。然后,选择所有内容并单击菜单栏中的格式。点击Untabify地区,并确保你做到了4列。 如果这不起作用,您的代码中可能早有语法错误 – ytpillai