使用 Vim Retab 解决 TabError:在缩进中使用制表符和空格不一致? [英] Use Vim Retab to solve TabError: inconsistent use of tabs and spaces in indentation?
问题描述
为新手问题道歉,但我已经阅读了手册,这个问题,并尝试了几次都没有我预期的结果.
Apologize for the newbie question, but I have read the manual, this question, and tried several times without results I expected.
所以我使用vim来编辑一个文件(附后).但是在运行时,我得到了 TabError:在缩进错误中不一致使用制表符和空格.
So I was using vim to edit a file (attached). But when running, I got the TabError: inconsistent use of tabs and spaces in indentation error.
这是我尝试过的:
- 用 Vim 打开文件.输入
:retab
和:x
.再次运行该文件.仍然收到 TabError 消息. - 再次打开文件并输入
:retab!
和:x
.再次运行该文件.仍然收到 TabError 消息. - 再次打开文件并输入
:retab!4
和:x
.再次运行该文件.这次它有效,但我不知道为什么?另外,文件缩进似乎过长.(我在此处读到编辑器可能会显示 8 个空格一个标签)
- Open the file with Vim. type
:retab
, and:x
. Run the file again. Still got the TabError message. - Open the file again and type
:retab!
and:x
. Run the file again. Still got the TabError message. - Open the file again and type
:retab! 4
and:x
. Run the file again. This time it works but I have no idea why? Plus, in the files indentation seems excessively long. (I read here that the editor might display 8 spaces for a tab)
我的问题是:
:retab
、:retab!
和:retab! 的作用是什么?4
是什么意思?
为什么 :retab
对我的文件不起作用?
Why doesn't :retab
work on my file?
#!/usr/bin/env python
#Reduce function for computing matrix multiply A*B
#Input arguments:
#variable n should be set to the inner dimension of the matrix product (i.e., the number of columns of A/rows of B)
import sys
import string
import numpy
#number of columns of A/rows of B
n = int(sys.argv[1])
#Create data structures to hold the current row/column values (if needed; your code goes here)
currentkey = None
alist = [] # list for elelents in A
blist = [] # list for elements in B
# input comes from STDIN (stream data that goes to the program)
for line in sys.stdin:
#Remove leading and trailing whitespace
line = line.strip()
#Get key/value
key, value = line.split('\t',1)
print(key, value)
#Parse key/value input (your code goes here)
key = (key.split(',', 1)[0], key.split(',',1)[1])
value = (value.split(',', 1)[0], value.split(',',1)[1], value.split(',',1)[2])
#If we are still on the same key...
if key==currentkey:
#Process key/value pair (your code goes here)
# store all values in a lisl
if value[0]=='A':
alist.append([value[1], value[2]])
else:
blist.append([value[1], value[2]])
#Otherwise, if this is a new key...
else:
#If this is a new key and not the first key we've seen, i.e. currentkey!=None
if currentkey:
#compute/output result to STDOUT (your code goes here)
alist = sorted(alist)
blist = sorted(blist)
newlist = [a[1]*b[1] for a,b in zip(alist, blist)]
res = newlist.sum()
print(currentkey, res)
currentkey = key
#Process input for new key (your code goes here)
推荐答案
只需在 Vim 中输入 :help retab
并阅读.我想我不能比帮助更好地解释它.也许你错过了可选的范围部分;使用 %
前缀应用于整个文件.:set list
也很有用,可以显示每个字符;这将显示制表符和行尾(使用 :set nolist
禁用)和 :set
没有值以查看当前值,例如 : 设置制表位
或后跟一些要设置的值.
Just type :help retab
in Vim and read. I think I can't explain it better than the help. Maybe you are missing the optional range part; use %
prefix to apply to the whole file. Also useful is :set list
to show you every chars; this will show you the tabs and line ends (disable with :set nolist
) and :set <name>
with no value to see the current value, ex : set tabstop
or followed by some value to set.
通过显示所有字符,使用 :set expandtab
和 :set noexpandtab
启用和禁用将制表符扩展为空格,设置制表位并使用 for ex.<代码>:retab!4你可以随意切换,只在tabs和空格之间切换,还可以改变tab列的宽度.
By showing all chars, enabling and disabling expansion of tabs into spaces with :set expandtab
and :set noexpandtab
, setting tabstop and by using for ex. :retab! 4
you can play your way around and switch from tabs only and spaces only, and change the tab column width.
这个链接,python的vim设置也可能有用
这篇关于使用 Vim Retab 解决 TabError:在缩进中使用制表符和空格不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!