Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with identation
#1
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>
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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.
Reply
#4
(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
Reply
#5
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020