-3

Sample from the file :

Employer: {
name:"Jack M", age:"213", phone:"11221"
}

Guest: {
name:"Alex K", age:"203", phone:"11111"
}

From that file, i need to export all the Guest names. Tried that:

file = "data.txt"
nameslist=[]

with open(file, "r") as f:
    i = f.read()

check = i.find('Guest: {')
while check != -1:
    i = i.replace('Guest: {', '\n') 
    i = i.split('\n')
    i = i[1]
    i = i.replace('name:"', '\n') 
    i = i.split('\n')
    i = i.replace('",' '\n')
    i = i.split('\n')
    global nameslist
    nameslist.append(i[0])
    i = i[1]
    check = i.find('Guest: {')

print(nameslist)

always have an error like that:


  File "asd.py", line 11
    i = i.split('\n')
                    ^
TabError: inconsistent use of tabs and spaces in indentation

What i am doing wrong ?

  • why there is a global in the middle ? if the code is not a part of the function ? – venkata krishnan Feb 24 '20 at 07:14
  • Does this answer your question? ["inconsistent use of tabs and spaces in indentation"](https://stackoverflow.com/questions/5685406/inconsistent-use-of-tabs-and-spaces-in-indentation) – razdi Feb 24 '20 at 07:15

2 Answers2

0

With python3 you can only either use tabs or spaces at the beginning of a line to mark indentation. Use your editor of choice to search/highlight 'tabs' or ' '. Then, may be, it is the best idea to replace a tab four spaces (using the replace tool of your editor).

Frank-Rene Schäfer
  • 3,182
  • 27
  • 51
0

What i am doing wrong ?

Not reading the error message? It is reasonably clear: when indenting (leading space before the code), sometimes you're using spaces and sometimes you're using tab characters. You must use either one or the other (spaces is usually recommended in Python). And should configure your text editor such that it always uses the right one.

Masklinn
  • 34,759
  • 3
  • 38
  • 57