Python Indentation Error [closed]

2024/11/16 7:31:29

the following code is returning some strange indentation error inside a for statement. The console is pointing the error at a list.append() method. It should be a silly misstake onetime I am new at Python. heh

import os
import sys
import re
import globfrom Text import Text
from Word import Word
from FileCategory import FileCategoryclass FileIO(object):def loadFile(self, filePath):newText = Text(os.path.basename(filePath))words=[]with open(filePath) as buff:content = buff.read().lower()re.sub("[^\w\s]", "", content)re.sub("[0-9]", "", content)words = content.Split(' ')for word in words:wordFound = next(auxWord for auxWord in newText.words if auxWord.textWord == word) if wordFound is None:newWord = Word(word, 1)newText.words.append(newWord)else:wordFound.countInText+=1return newTextdef loadFilesFromDirectory(self, path):newCategory = FileCategory()files=[]os.chdir(path)for file in glob.glob("*.txt"): files.append(file)for filePath in files:newFile = loadFile(filePath)if newFile is not None:newCategory.files.append(newFile)return newCategory

Log:

Traceback (most recent call last):File "aprendizadMaq.py", line 6, in <module>from FileIO import FileIOFile "/home/adolfosrs/Dropbox/7º Semestre/IA/T2/pyAprendizadMaq/FileIO.py", line 38files.append(file)^
IndentationError: expected an indented block

Any Idea?

Answer

The problem arises from the fact that python doesn't like it when you mix tabs and spaces for indentation. Python relies on whitespace a LOT to figure out the levels of indentation, and hence scope.

As a result, when you have a line that has two TABs on it, followed by a line with 4 SPACEs and a TAB, python proceeds to freak out and tell you that your indentation is off.

TL;DR: don't mix tabs and spaces. Stick to one or the other, and you should be fine.

Fun aside: if you really /really/ REALLY wanted to ignore this tip and try to mix the two, I believe TABs are considered to be the equivalent of 8 SPACEs. So you could in theory do some indentation math there, to figure out how much to indent each line by, with a mix of the two. But this is HIGHLY unrecommended, and I'm probably going to burn in hell just for saying it

https://en.xdnf.cn/q/120350.html

Related Q&A

Allow form submission only once a day django

I want to allow users to submit a django form once, and only once everyday. After submitting the form, the form wouldnt even show (server-side checkings, I dont want to use JS or client side thing; eas…

Counting percentage of element occurence from an attribute in a class. Python

I have a class called transaction that have these attributes Transaction([time_stamp, time_of_day, day_of_month ,week_day, duration, amount, trans_type, location])an example of the data set is as sucht…

AWS | Syntax error in module: invalid syntax

I have created python script which is uploaded as a zip file in AWS Lambda function with stompy libraries bundled in them.Logs for python 2.7:-Response: nullRequest ID: "c334839f-ee46-11e8-8970-61…

Algorithm for finding if an array is balanced [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Merging two dataframes in python pandas [duplicate]

This question already has answers here:Pandas Merging 101(8 answers)Closed 5 years ago.I have a dataframe A:a 1 a 2 b 1 b 2Another dataframe B:a 3 a 4 b 3I want my result dataframe to be like a 1 3 a …

Searching for the best fit price for multiple customers [duplicate]

This question already has an answer here:Comparing multiple price options for many customers algorithmically(1 answer)Closed 10 years ago.A restatement of Comparing multiple price options for many cust…

Can we chain the ternary operator in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.The com…

evaluate a python string expression using dictionary values

I am parsing a text file which contain python "string" inside it. For e.g.:my_home1 in houses.split(,) and 2018 in iphone.split(,) and 14 < maskfor the example above, I wrote a possible di…

How to simply get the master volume of Windows in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 1 year ago.Improve …

Python: Adding positive values in a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…