我的python脚本中的代码有时会突然变成,我想您可以说已经损坏了。懒惰会突然改变,使我的程序失败。
如果我使用cat来查看文件,我就会发现I是错误的。但在VIM内,它表现得很好。这是输出和设置,
有什么主意吗??
通过'cat -e'
validate_hostname = RegexValidator(regex=r'[a-zA-Z0-9-_]*\.[a-zA-Z]{2,6}',message="Enter a valid hostname.")$
validate_hostname(host_input)$
except ValidationError, e:$
print type(e)$
print str(e[0])$
error = str(e)$
else:$
error = "Please complete all fields." $
$
print error$
return [error,host_input,record_input]$in VIM,
validate_hostname = RegexValidator(regex=r'[a-zA-Z0-9-_]*\.[a-zA-Z]{2,6}',message="Enter a valid hostname.")
validate_hostname(host_input)
except ValidationError, e:
print type(e)
print str(e[0])
error = str(e)
else:
error = "Please complete all fields."
print error
return [error,host_input,record_input]我的.vimrc看起来
syntax on
se bg=dark
set tabstop=4 " insert 4 spaces when a tab is pressed
set shiftwidth=4 " change the number of space characters inserted for indentation
set expandtab " insert spaces whenver a tab key is pressed发布于 2013-09-21 08:19:45
看起来你有混合的空格和制表符。代码在vim和cat -e (或简单地说是less)中看起来不同,因为它们对选项卡使用不同的宽度,这是因为您的set tabstop=4。
如果在vim中它看起来很好,那么执行:retab就应该修复它:它将用您看到的空格替换制表符字符。结果看起来是一样的,但是所有的标签字符都会消失。
在执行tabstop之前,拥有正确的retab值是很重要的。例如,如果您有相反的问题--代码在less中看起来是正确的,但是在vim中是坏的,并且在这种状态下执行:retab,这将破坏Python。
请看这篇关于vim中选项卡的精彩文章:
http://vimcasts.org/episodes/tabs-and-spaces/
特别是,我认为您应该将这些设置添加到.vimrc中。
set softtabstop=4
set smarttab发布于 2013-09-21 14:52:55
特别是在Python中,空格很重要,不应该混合制表符和空格。即使您已经在Vim中仔细地设置了缩进设置(甚至可能在每个文件中都包含了modelines来设置缩进),其他编辑该文件的用户也可能不会那么小心。
因此,我编写了IndentConsistencyCop插件,它验证缩进,并在不一致时抱怨。插件页面有指向其他插件的链接。
https://stackoverflow.com/questions/18930320
复制相似问题