gpt4 book ai didi

python - 使用Vim Retab解决TabError : inconsistent use of tabs and spaces in indentation?

转载 作者:行者123 更新时间:2023-11-28 22:20:44 30 4
gpt4 key购买 nike

为新手问题道歉,但我已经阅读了手册,this问题,并尝试了几次都没有我预期的结果。

所以我正在使用 vim 来编辑一个文件(附件)。但是运行的时候报TabError: inconsistent use of tabs and spaces in indentation错误。

这是我尝试过的:

  • 用 Vim 打开文件。键入 :retab:x。再次运行该文件。仍然收到 TabError 消息。
  • 再次打开文件并键入 :retab!:x。再次运行该文件。仍然收到 TabError 消息。
  • 再次打开文件并输入 :retab! 4:x。再次运行该文件。这次成功了,但我不知道为什么?另外,在文件中缩进似乎过长。 (我读到 here 编辑器可能会为制表符显示 8 个空格)

我的问题是:

  • 什么是 :retab:retab!:retab! 4 是什么意思?

  • 为什么 :retab 对我的文件不起作用?

    #!/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)

最佳答案

只需输入 :help retab在 Vim 中阅读。我想我无法比帮助更好地解释它。也许您缺少可选范围部分;使用 %应用于整个文件的前缀。同样有用的是 :set list向你展示每一个字符;这将向您显示制表符和行尾(使用 :set nolist 禁用)和 :set <name>没有查看当前值的值,例如 : set tabstop或后跟一些要设置的值。

通过显示所有字符,使用 :set expandtab 启用和禁用将制表符扩展到空格中和 :set noexpandtab ,设置 tabstop 并使用 for ex。 :retab! 4您可以随意切换,从仅制表符和仅空格切换,并更改制表符列的宽度。

此链接,vim settings for python也可能有用

关于python - 使用Vim Retab解决TabError : inconsistent use of tabs and spaces in indentation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48735671/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com