Python IndentationError:意外缩进 [英] Python IndentationError: unexpected indent
问题描述
这是我的代码...我收到缩进错误,但我不知道为什么会发生.
Here is my code ... I am getting indentation error but i don't know why it occurs.
->
# loop
while d <= end_date:
# print d.strftime("%Y%m%d")
fecha = d.strftime("%Y%m%d")
# set url
url = 'http://www.wpemergencia.omie.es//datosPub/marginalpdbc/marginalpdbc_' + fecha + '.1'
# Descargamos fichero
response = urllib2.urlopen(url)
# Abrimos fichero
output = open(fname,'wb')
# Escribimos fichero
output.write(response.read())
# Cerramos y guardamos fichero
output.close()
# fecha++
d += delta
推荐答案
使用以下程序运行程序
python -t script.py
如果制表符和空格混合使用,这将警告您.
This will warn you if you have mixed tabs and spaces.
在* nix系统上,您可以通过运行查看选项卡的位置
On *nix systems, you can see where the tabs are by running
cat -A script.py
,您可以使用以下命令将制表符自动转换为4个空格
and you can automatically convert tabs to 4 spaces with the command
expand -t 4 script.py > fixed_script.py
PS.进行编程时,请务必使用编程编辑器(例如emacs,vim),而不要使用文字处理器.使用编程编辑器不会遇到这个问题.
PS. Be sure to use a programming editor (e.g. emacs, vim), not a word processor, when programming. You won't get this problem with a programming editor.
PPS.对于emacs用户, M-x whitespace-mode
将在emacs缓冲区中显示与cat -A
相同的信息!
PPS. For emacs users, M-x whitespace-mode
will show the same info as cat -A
from within an emacs buffer!
这篇关于Python IndentationError:意外缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!