简体   繁体   中英

Python "inconsistent use"

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 ?

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM