将Python2语法移植到Python3

将Python2语法移植到Python3,python,python-3.x,python-2.x,Python,Python 3.x,Python 2.x,我正在尝试在python3中运行以下代码,但它是为python2编写的: f = open(filename, 'r') self.lines = f.readlines() f.close() if self.lines[-1] != "\n" : self.lines.append("\n") 但我得到了以下错误: File "randline.py", line 32 if self.lines[-1] != "\n" :

我正在尝试在python3中运行以下代码,但它是为python2编写的:

f = open(filename, 'r')
self.lines = f.readlines()
f.close()
if self.lines[-1] != "\n" :
    self.lines.append("\n")
但我得到了以下错误:

  File "randline.py", line 32
    if self.lines[-1] != "\n" :
                              ^
TabError: inconsistent use of tabs and spaces in indentation

你能帮我找出正确的语法吗?

Python 2允许混合空格和制表符。所以你可以有如下缩进:

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[this is a tab it counts like eight spaces             ][space][space]print(each)
[space][space][space][space][space][space][space][space]print("Done!")
Python2中的第2行和第4行具有相同的缩进级别,但第2行使用制表符,第4行使用空格。打印到控制台时,它将如下所示:

def foo()
        for each in range(5):
          print(5)
        print("Done!")
但大多数编辑器允许您设置选项卡的空格数。将其设置为4,则得到以下结果:

def foo()
    for each in range(5):
      print(5)
        print("Done!")
缩进仍然是一样的,但现在看起来缩进是错误的

因此,Python 3不允许以不同的方式缩进相同的缩进级别(即第2行和第4行)。您仍然可以混合制表符和空格,但不能在同一缩进级别。这意味着

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[this is a tab it counts like eight spaces             ][space][space]print(each)
[this is a tab it counts like eight spaces             ]print("Done!")
会起作用的,也会起作用的

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[space][space][space][space][space][space][space][space][space][space]print(each)
[this is a tab it counts like eight spaces             ]print("Done!")
唯一能使缩进看起来奇怪的方法是,将制表符设置为大于8个空格,然后缩进不仅看起来明显不正确,还会注意到制表符将缩进12个空格(在下面的示例中),因此您意识到插入的是制表符,而不是4个空格

def foo():
            for each in range(5):
          print(each)
            print("Done!")

当然,你所有问题的解决方案都写在评论中,永远不要使用标签。我不知道为什么Python3仍然允许使用制表符,真的没有什么好的理由。

Python2允许混合使用空格和制表符。所以你可以有如下缩进:

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[this is a tab it counts like eight spaces             ][space][space]print(each)
[space][space][space][space][space][space][space][space]print("Done!")
Python2中的第2行和第4行具有相同的缩进级别,但第2行使用制表符,第4行使用空格。打印到控制台时,它将如下所示:

def foo()
        for each in range(5):
          print(5)
        print("Done!")
但大多数编辑器允许您设置选项卡的空格数。将其设置为4,则得到以下结果:

def foo()
    for each in range(5):
      print(5)
        print("Done!")
缩进仍然是一样的,但现在看起来缩进是错误的

因此,Python 3不允许以不同的方式缩进相同的缩进级别(即第2行和第4行)。您仍然可以混合制表符和空格,但不能在同一缩进级别。这意味着

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[this is a tab it counts like eight spaces             ][space][space]print(each)
[this is a tab it counts like eight spaces             ]print("Done!")
会起作用的,也会起作用的

def foo():
[this is a tab it counts like eight spaces             ]for each in range(5):
[space][space][space][space][space][space][space][space][space][space]print(each)
[this is a tab it counts like eight spaces             ]print("Done!")
唯一能使缩进看起来奇怪的方法是,将制表符设置为大于8个空格,然后缩进不仅看起来明显不正确,还会注意到制表符将缩进12个空格(在下面的示例中),因此您意识到插入的是制表符,而不是4个空格

def foo():
            for each in range(5):
          print(each)
            print("Done!")

当然,你所有问题的解决方案都写在评论中,永远不要使用标签。我不知道为什么Python 3仍然允许使用制表符,真的没有什么好的理由。

在编辑器中使用显示空白。删除所有制表符。使用4个空格键解决了我的问题。添加您的评论作为答案,以便我投票并接受!:)在编辑器中使用“显示空白”。删除所有选项卡。使用4个空格键解决了我的问题。添加您的评论作为答案,以便我投票并接受!:)伟大的文章,直到最后(巨魔诱饵)条款。使用制表符有很多很好的理由,比如按比例宽度字体编码,或者与其他语言保持一致,或者有一个单一字符映射到一个结构,或者在编辑器中更容易编辑,而不将
四个空格作为缩进
作为单一结构。还有其他的原因,这就是为什么这个话题争论了这么久,以及为什么我仍然使用tabs编写代码,尽管我承认这艘船是为Python设计的。另外,今天我了解到,Python 3确实允许在一个文件中混合制表符和空格,只是不允许在一个给定的块中混合(正如您所说的那样)。很棒的文章,直到最后(巨魔诱饵)条款。使用制表符有很多很好的理由,比如按比例宽度字体编码,或者与其他语言保持一致,或者有一个单一字符映射到一个结构,或者在编辑器中更容易编辑,而不将
四个空格作为缩进
作为单一结构。还有其他的原因,这就是为什么这个主题争论了这么久,以及为什么我仍然使用tabs编写代码,尽管我承认这艘船是为Python而航行的。另外,今天我了解到,Python 3确实允许在文件中混合制表符和空格,只是不允许在给定的块中混合(正如您正确指出的)。