![](/img/trans.png)
[英]Pycharm TabError: inconsistent use of tabs and spaces in indentation
[英]Use Vim Retab to solve TabError: inconsistent use of tabs and spaces in indentation?
為新手問題道歉,但我已經閱讀了手冊, 這個問題,並嘗試了幾次沒有我預期的結果。
所以我使用vim來編輯一個文件(附后)。 但是在運行時,我得到了 TabError:在縮進錯誤中不一致使用制表符和空格。
這是我嘗試過的:
:retab
和:x
。 再次運行該文件。 仍然收到 TabError 消息。:retab!
和:x
。 再次運行該文件。 仍然收到 TabError 消息。:retab! 4
:retab! 4
和:x
。 再次運行該文件。 這次它有效,但我不知道為什么? 另外,文件縮進似乎過長。 (我在這里讀到編輯器可能會為一個選項卡顯示 8 個空格)我的問題是:
什么:retab
, :retab!
, 和:retab! 4
: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 (ie, 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, ie 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 <name>
沒有值以查看當前值,例如: set tabstop
或后跟一些要設置的值。
通過顯示所有字符,使用:set expandtab
和:set noexpandtab
啟用和禁用選項卡擴展到空格,設置 tabstop 和使用 for ex。 :retab! 4
:retab! 4
您可以隨意切換,僅從制表符和空格切換,並更改制表符列的寬度。
這個鏈接, python 的 vim 設置也可能有用
退出
:%s/\t/ /g
這意味着用 4 個空格替換制表符。 按 Enter 鍵
:wq
如果您已復制粘貼的代碼並開始編輯它,則僅嘗試運行:retab
可能無法正常工作。
您可以先嘗試設置 vim/vi 設置:
sudo mkdir ~/.vim && sudo touch ~/.vim/vimrc
來創建設置文件vimrc
以添加以下代碼段 set softtabstop=4
set tabstop=4
set shiftwidth=4
set expandtab
:retab
對於新手問題,我們深表歉意,但是我已經閱讀了該問題手冊,並嘗試了幾次,但都沒有得到預期的結果。
因此,我使用vim編輯文件(附加)。 但是在運行時,我得到了TabError:縮進中的制表符和空格不一致使用錯誤。
這是我嘗試過的:
:retab
和:x
。 再次運行文件。 仍然收到TabError消息。:retab!
和:x
。 再次運行文件。 仍然收到TabError消息。:retab! 4
:retab! 4
和:x
。 再次運行文件。 這次可以用,但是我不知道為什么? 另外,在文件中縮進似乎過長。 (我在這里讀到,編輯器可能在一個選項卡上顯示8個空格)我的問題是:
:retab
, :retab!
是什么:retab!
和:retab! 4
: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 (ie, 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, ie 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.