Reputation: 1524
Here is my code... I am getting an 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
Upvotes: 29
Views: 129591
Reputation: 20206
Simply copy your script, and put it under """" your entire code """
"...
Specify this line in a variable... Like,
a = """ your entire code """
print a.replace(' ',' ') # First 4 spaces tab, second four spaces from the space bar
print a.replace('Here please press the tab button. It will insert some space"," Here simply press the space bar four times")
# Here we are replacing tab space by four character space as per the PEP 8 style guide...
Now execute this code in Sublime Text using Ctrl + B. Now it will print the indented code in the console. That's it.
Upvotes: 0
Reputation: 101
Find all tabs and replaced by 4 spaces in Notepad++. It worked.
Upvotes: 9
Reputation: 11624
You can't mix tab and spaces for indentation. The best practice is to convert all tabs to spaces.
How can we fix this? Well, just delete all the spaces/tabs before each line and convert them uniformly either to tabs or spaces, but don't mix them. The best solution: enable the option in your editor to convert any tabs to spaces automagically.
Also be aware that your actual problem may lie in the lines before this block, and Python throws the error here, because of a leading invalid indentation which doesn't match the following indentations!
Upvotes: 3
Reputation: 881007
Run your program with
python -t script.py
This will warn you if you have mixed tabs and spaces.
On Unix-like systems, you can see where the tabs are by running
cat -A script.py
And you can automatically convert tabs to 4 spaces with the command
expand -t 4 script.py > fixed_script.py
PS. Be sure to use a programming editor (e.g., Emacs and Vim), not a word processor, when programming. You won't get this problem with a programming editor.
PPS. For Emacs users, M-x whitespace-mode
will show the same information as cat -A
from within an Emacs buffer!
Upvotes: 50
Reputation: 1594
Check if you mixed tabs and spaces. That is a frequent source of indentation errors.
Upvotes: 8