我試圖將下面的python代碼複製粘貼到我的macvim中,並且出現「Unexpected indent」。Python在我的MacVim上意外縮進
## Can build up a dict by starting with the the empty dict {}
## and storing key/value pairs into the dict like this:
## dict[key] = value-for-that-key
dict = {}
dict['a'] = 'alpha'
dict['g'] = 'gamma'
dict['o'] = 'omega'
print dict ## {'a': 'alpha', 'o': 'omega', 'g': 'gamma'}
print dict['a'] ## Simple lookup, returns 'alpha'
dict['a'] = 6 ## Put new key/value into dict
'a' in dict ## True
## print dict['z'] ## Throws KeyError
if 'z' in dict: print dict['z'] ## Avoid KeyError
print dict.get('z') ## None (instead of KeyError)
我檢查是否因爲混合了製表符和空格,但什麼也沒找到;
python -tt new.py
File "new.py", line 4
dict = {}
^
IndentationError: unexpected indent
還發現,可能是因爲自動縮進通過我的Vim的,並試圖通過使用
:set paste
這也不起作用禁用它。下面是我的.vimrc文件
set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smarttab
set retab
set paste
filetype indent on
filetype on
filetype plugin on
這是文件
00000000 20 20 23 23 20 43 61 6e 20 62 75 69 6c 64 20 75 | ## Can build u|
00000010 70 20 61 20 64 69 63 74 20 62 79 20 73 74 61 72 |p a dict by star|
00000020 74 69 6e 67 20 77 69 74 68 20 74 68 65 20 74 68 |ting with the th|
00000030 65 20 65 6d 70 74 79 20 64 69 63 74 20 7b 7d 0a |e empty dict {}.|
00000040 20 20 23 23 20 61 6e 64 20 73 74 6f 72 69 6e 67 | ## and storing|
00000050 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 | key/value pairs|
00000060 20 69 6e 74 6f 20 74 68 65 20 64 69 63 74 20 6c | into the dict l|
00000070 69 6b 65 20 74 68 69 73 3a 0a 20 20 23 23 20 64 |ike this:. ## d|
00000080 69 63 74 5b 6b 65 79 5d 20 3d 20 76 61 6c 75 65 |ict[key] = value|
00000090 2d 66 6f 72 2d 74 68 61 74 2d 6b 65 79 0a 20 20 |-for-that-key. |
000000a0 64 69 63 74 20 3d 20 7b 7d 0a 20 20 64 69 63 74 |dict = {}. dict|
000000b0 5b 27 61 27 5d 20 3d 20 27 61 6c 70 68 61 27 0a |['a'] = 'alpha'.|
000000c0 20 20 64 69 63 74 5b 27 67 27 5d 20 3d 20 27 67 | dict['g'] = 'g|
000000d0 61 6d 6d 61 27 0a 20 20 64 69 63 74 5b 27 6f 27 |amma'. dict['o'|
000000e0 5d 20 3d 20 27 6f 6d 65 67 61 27 0a 0a 20 20 70 |] = 'omega'.. p|
000000f0 72 69 6e 74 20 64 69 63 74 20 20 23 23 20 7b 27 |rint dict ## {'|
00000100 61 27 3a 20 27 61 6c 70 68 61 27 2c 20 27 6f 27 |a': 'alpha', 'o'|
00000110 3a 20 27 6f 6d 65 67 61 27 2c 20 27 67 27 3a 20 |: 'omega', 'g': |
00000120 27 67 61 6d 6d 61 27 7d 0a 0a 20 20 70 72 69 6e |'gamma'}.. prin|
00000130 74 20 64 69 63 74 5b 27 61 27 5d 20 20 20 20 20 |t dict['a'] |
00000140 23 23 20 53 69 6d 70 6c 65 20 6c 6f 6f 6b 75 70 |## Simple lookup|
00000150 2c 20 72 65 74 75 72 6e 73 20 27 61 6c 70 68 61 |, returns 'alpha|
00000160 27 0a 20 20 64 69 63 74 5b 27 61 27 5d 20 3d 20 |'. dict['a'] = |
00000170 36 20 20 20 20 20 20 20 23 23 20 50 75 74 20 6e |6 ## Put n|
00000180 65 77 20 6b 65 79 2f 76 61 6c 75 65 20 69 6e 74 |ew key/value int|
00000190 6f 20 64 69 63 74 0a 20 20 27 61 27 20 69 6e 20 |o dict. 'a' in |
000001a0 64 69 63 74 20 20 20 20 20 20 20 20 20 23 23 20 |dict ## |
000001b0 54 72 75 65 0a 20 20 23 23 20 70 72 69 6e 74 20 |True. ## print |
000001c0 64 69 63 74 5b 27 7a 27 5d 20 20 20 20 20 20 20 |dict['z'] |
000001d0 20 20 20 20 20 20 20 20 20 20 20 23 23 20 54 68 | ## Th|
000001e0 72 6f 77 73 20 4b 65 79 45 72 72 6f 72 0a 20 20 |rows KeyError. |
000001f0 69 66 20 27 7a 27 20 69 6e 20 64 69 63 74 3a 20 |if 'z' in dict: |
00000200 70 72 69 6e 74 20 64 69 63 74 5b 27 7a 27 5d 20 |print dict['z'] |
00000210 20 20 20 20 23 23 20 41 76 6f 69 64 20 4b 65 79 | ## Avoid Key|
00000220 45 72 72 6f 72 0a 20 20 70 72 69 6e 74 20 64 69 |Error. print di|
00000230 63 74 2e 67 65 74 28 27 7a 27 29 20 20 23 23 20 |ct.get('z') ## |
00000240 4e 6f 6e 65 20 28 69 6e 73 74 65 61 64 20 6f 66 |None (instead of|
00000250 20 4b 65 79 45 72 72 6f 72 29 0a | KeyError).|
0000025b
我被愚蠢的和失去了一些東西很簡單的hexdump都可以?你會推薦我進入基於Python的IDE如PyCharm嗎?
'dict'是實現python字典的類的名稱。你爲什麼分配它?我不知道這是否是你的問題,但我要做的第一件事是使用不同的名字。 –
我嘗試過使用不同的名稱,但沒有幫助。這段代碼來自Google Python類,我只是複製粘貼。謝謝。 – h1py
然後它很奇怪。也許做文件的二進制轉儲?我一直在Vim中編輯Python代碼,之前沒有隱藏縮進。 –