Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Python3 is telling me I have an error in my indentation. I've tried about a million different indentations and I am lost. It's not pointing to the error directly, and just pointing to a parenthesis, leaving me to figure it out on my own.
import os
for root, dirs, files in os.walk('C:\\Users\\Tom\\Desktop'):
for file in files:
if file.endswith('.txt'):
f = open("test.txt", "r+")
f.seek(0)
for line in f:
a = f.read()
f.seek(0)
for char in a:
o = ord(char)
f.write(str(o))
f.truncate()
Apologies, I forgot to include the error message.
File "C:\Users\Tom\Desktop\Search.py", line 6
f = open("test.txt", "r+")
^
TabError: inconsistent use of tabs and spaces in indentation
I loaded the text from your question into a text editor (vim) and showed invisible characters, which renders this.
Here, spaces show as space, and tab shows as ^I. As you can see, your second for and first if lines are indented with spaces, and the rest of the file is indented with tabs.
In the general sense, this creates a real mess in Python, where indentation is syntactically significant to the program structure.
In Python 3 specifically, mixing tabs and spaces as indentation is a fatal compile error. That is what you've encountered (TabError).
See PEP-8, which suggests using spaces only, never tabs, and using a 4-space indent.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have regular expression \n([\d]) that can match this following text:
Then I want to replace that matched text with first group or $1 in Visual Studio Code. This is the result:
I want the same idea in python, which I already make this code.
import re
file = "out FCE.txt"
pattern = re.compile(".+")
for i, line in enumerate(open(file)):
for match in re.finditer(pattern, line):
print(re.sub(r"\n([\d])", r"\1", match.group()))
But that code does nothing to it. Which mean the result is still the same as the first picture. Newlines and the line with numbers at first character are not removed. I already read this answer, that python is using \1 not $1. And yes, I want to keep the whitespaces between in order to be neat as \t\t\t.
Sorry if my explanation is confusing and also my english is bad.
The problem here is that you are reading the file line by line. In each loop of for i, line in enumerate(open(file)):, re.sub accesses only one line, and therefore it cannot see whether the next line starts with a digit.
Try instead:
import re
file = "out FCE.txt"
with open(file, 'r') as f:
text = f.read()
new_text = re.sub(r"\n([\d])", r"\1", text)
print(new_text)
In this code the file is read as a whole (into the variable text) so that re.sub now sees whether the subsequent line starts with a digit.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
>>> for link in soup.find_all('a'):
... print link
File "<stdin>", line 2
print link
^
IndentationError: expected an indented block
The correct indentation should be:
for link in soup.find_all('a'):
print(link)
Try this snippet to understand the need for indentation
for x in range(3):
print("Inside the loop", x)
print("Outside the loop, this print is run only once")
This notion is well explained in the beginning of Python tutorial:
The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm using geany and I get the following error
File "autoblog2.py", line 9
htmlfile = urllib.urlopen(url)
^
IndentationError: unindent does not match any outer indentation level
here is my code.
import urllib
import re
symbols_list = ["aapl","spy","goog","nflx"]
i = 0
while i<len(symbols_list):
url = 'https://uk.finance.yahoo.com/q?s='+symbols_list[i]
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_aapl">(.+?)'+symbols_list[i]'</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print 'the price of' +symbols_list[i]
i+=1
I don't get any errors when I run the same code on a single url. I've only had since trying it with a while loop, i'm using python 2
This can happen when you edit one script with two editors.
Your indent settings can differ from editor to editor.
Take a look at the script with another editor.
If the script has the same indents in other editors the only way is to remove all indents and add them again.
I would recommend the python-idle.
It should show the indents like the interpreter reads them.
Good Luck.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When i make error?
Don't see errors. Please, help me.
datafile = file('c:\\Users\\username\\Desktop\\test.txt')
for line in datafile:
if '5256' in line:
GLOBACCESS[jid]=100
reply('private', source, u'You license is valid!')
else:
reply('private', source, u'Incorrect password/jid')
Lines 2 and 3 are using spaces while everything else is using tabs. You always need to use the same, and should choose spaces per pep 8
Line 2 shouldn't be indented at all, with spaces or with tabs
for loop shouldn't be indented.
datafile = file('c:\\Users\\username\\Desktop\\test.txt')
for line in datafile:
if '5256' in line:
GLOBACCESS[jid]=100
reply('private', source, u'You license is valid!')
else:
reply('private', source, u'Incorrect password/jid')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Can anyone explain why I am getting a syntax error on the below, my editor won't accept the "else":
for i in self.jobs:
with open('postcodes.csv', 'rb') as f: #see http://docs.python.org/2/library/csv.html#csv-examples
reader = csv.reader(f)
for row in reader:
if row[0] == self.jobs[i][3]:
self.jobs[i].append((row[1],row[2]))
else:
self.jobs[i].append("Latitude & Longitude not available")
Your indentation is inconsistent because it's a mixture of space and tab characters, as you can see in the screenshot below where I've turned on a "visible whitespace" mode in my editor.
I suggest you convert all the tabs in your code to space characters -- perhaps your editor has a command to do that -- and avoid mixing the two in the future. Since, as you're discovering the hard way, it can cause bizarre and hard-to-diagnosis errors.
In the future, consider evaluating your code for PEP-8 deviations:
http://pych.atomidata.com/