我正在尝试将以下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
这是文件的
的hexdump00000000 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
我是愚蠢而且缺少一些非常简单的东西?你会建议我进入像PyCharm这样基于Python的IDE吗?
答案 0 :(得分:1)
在你的hexdump中,在第一行代码之前有两个空格。这意味着你的初始缩进是两个空格,而python根本没有空格(或制表符)。字节0x9e和0x9f是空格,字节0xa0是dict = {}
中的'd'。
如果选项'hlsearch'已启用,则可以使用/^\s*
查看起始空格。