首页 > 解决方案 > Python“使用不一致”

问题描述

来自文件的样本:

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

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

从该文件中,我需要导出所有来宾名称。试过了:

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)

总是有这样的错误:


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

我做错了什么?

标签: pythonsorting

解决方案


我做错了什么?

没有阅读错误信息?相当清楚:在缩进(代码前的前导空格)时,有时您使用空格,有时您使用制表符。您必须使用其中之一(Python 中通常建议使用空格)。并且应该配置您的文本编辑器,使其始终使用正确的编辑器。


推荐阅读