IndentationError: unexpected indent python
来源:互联网 发布:头戴式耳机 知乎 编辑:程序博客网 时间:2024/05/16 08:51
转自:http://dikar.iteye.com/blog/308934
这哥们总结的挺好的,所以转载过来备忘,在此表示感谢。
原文如下:
都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格。
以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下tab和空格了”。
在windows上你可以用editplus看的,虽然我不知道怎么显示出tab,但是他能显示你的文件是没对齐的,我是在ubuntu上编写python的。我用的是vim,你可以设置下vim中tab用空格替换,我在前一篇文章里写过关于这个设置的问题,我现在在补充一下,之前是在系统目录下设置的,现在我要在home目录下设置;
首先 cd ~(到你的home目录)
然后 ls -a(显示隐藏的文件)
找到.vimrc(记得是带个点前缀的)
然后编辑这个文件
找到如下的命令:
16 set autoindent
17 set smartindent
19 set tabstop=4
20 set shiftwidth=4
21 set softtabstop=4
22 set noexpandtab
然后在set tabstop=4前面加上set expandtab同时你需要把set noexpandtab这个注释掉,怎么注释呢,就是加个双引号。我没试过直接注释不加set expandtab,我想也可以(难道默认是expandtab)。这样你可以放心使用VIM了。
当然我并不是想就这样完事,我其实想看下我的代码到底哪里会有问题呢,就需要看下他的格式了,主要是看tab,这个可以同样在刚才那个文件里设置,在刚才设置的下面比如说
在set softtabstop=4下面加上一句set list。这句就可以让你的vim打开的文件显示出tab了,当然也许你觉得显示的不好看,他显示的样子是“^I”一个数字键6上那个符号和一个大写的I表示一个tab,这个不会显示出空格。他还会显示出段落的起始符^和终止符$,如果不知道这2个符号可以看下正则表达式。
以上2点设置以后,我想以后你就不会遇到IndentationError: unexpected indent这个很烦人的错误了。
以上的set命令都可以单独使用,但是只对当前打开的vim有效,你可以按下shift加冒号,然后输入set XXX,回车然后就有效果了,但是你要再次进入编辑模式才能看到。
希望分享给每个写python的人。
- IndentationError: unexpected indent python
- IndentationError: unexpected indent python
- IndentationError: unexpected indent python
- IndentationError: unexpected indent python
- python:IndentationError: unexpected indent
- IndentationError: unexpected indent python
- IndentationError: unexpected indent python
- Python的IndentationError: unexpected indent
- python错误:IndentationError: unexpected indent
- python报"IndentationError: unexpected indent"
- Python错误IndentationError: unexpected indent
- python的IndentationError: unexpected indent python
- python报"IndentationError: unexpected indent"的解决方法.
- python报错:IndentationError: unexpected indent
- Python编程错误之IndentationError: unexpected indent
- Python 报错:IndentationError: unexpected indent
- IndentationError: unexpected indent python格式问题
- python报"IndentationError: unexpected indent"的解决方法.
- springMVC调微信接口实现关注时消息回复和自动回复功能
- 搭建Hadoop full distributed cluster步骤
- redis数据结构运用场景
- 如何安装apk
- Linux 技巧:让进程在后台可靠运行的几种方法
- IndentationError: unexpected indent python
- 百度重度依赖者谈谈恶意点击
- Ubuntu Navicat for MySQL安装以及破解方案
- win7_oracle11g_64位连接32位PLSQL_Developer
- mysql in 排序 也可以按in里面的顺序来排序
- Android Canvas
- 一个80后程序员的转行创业日记10
- 完全卸载mysql步骤
- Bellman_ford算法