Python Forum
Help with identation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with identation (/thread-5357.html)



Help with identation - pythonbabe - Sep-30-2017

Following python program is giving indentation error. Please have a look at the for loop section and suggest me what i am doing wrong.
Python Version : 3.5

#/usr/bin/env python

file='/home/map'
F=open(file)
file_content=F.readlines()
length=len(file_content)
print('No of lines in input file is :', length)

tofind='<map>'
tofind1='</map>'

with open(file) as myFile:
    for num, line in enumerate(myFile, 1):
                if tofind in line:
                        print ('found at line: ', num)
                        if tofind1 in line:
                                print('found at line: ', num)
Error is:

Error:
File "linenumber.py", line 27 if tofind1 in line: ^ TabError: inconsistent use of tabs and spaces in indentation
Input File content is:
Some of the lines has white spaces in front of the content. I don't know how to paste the content as is.
I used the quote section here. But no help.

Quote:<map>
/apps/information
/apps/information/one
/apps/education
</map>
king

queen
god
parents
<map>
/dir
/tmp
/
/var/tmp
</map>



RE: Help with identation - metulburr - Sep-30-2017

whatever editor you use, make sure you set it to 4 spaces for indent, and do not use tabs. In that way, when you press tab, it actually inserts 4 spaces.


RE: Help with identation - pythonbabe - Sep-30-2017

with open(file) as myFile:
    for num, line in enumerate(myFile, 1):
        if tofind in line:
            print('found at line: ', num)
        if tofind1 in line:
            print('found at line: ', num)
No Luck even after i changed as above.


RE: Help with identation - snippsat - Sep-30-2017

(Sep-30-2017, 05:20 AM)pythonbabe Wrote: No Luck even after i changed as above.
You should not get TabError with that code.
Test:
tofind = '<map>'
tofind1 = '</map>'

with open('in.txt') as my_file:
    for num, line in enumerate(my_file, 1):
        if tofind in line:
            print(f'{tofind} at line: {num}')
        if tofind1 in line:
            print(f'{tofind1} at line: {num}')
Output:
<map> at line: 1 </map> at line: 5 <map> at line: 11 </map> at line: 16



RE: Help with identation - Larz60+ - Sep-30-2017

I noticed the error that you posted was on line 27.
The code snippet that you showed only had 17 lines.
This is not possible if all code was supplied.
Also the error traceback is not complete.
please:
  • make sure you are running the proper script
  • Show the entire error traceback, verbatim
  • if first item above is not an issue, show complete code
Thanks