python - 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
我做错了什么?
解决方案
我做错了什么?
没有阅读错误信息?相当清楚:在缩进(代码前的前导空格)时,有时您使用空格,有时您使用制表符。您必须使用其中之一(Python 中通常建议使用空格)。并且应该配置您的文本编辑器,使其始终使用正确的编辑器。
推荐阅读
- postgresql - 使用 Postgresql,计算长度大于 5 的表中的连续数字
- java - 有效负载文档大小大于最大值 16793600
- java - 我找不到 mainactivity.java 文件和布局文件夹
- sql - 通过左外连接的标题将虚拟行显示为组
- reactjs - 延迟一个字符更新状态
- javascript - 刷新后如何保持复选框处于活动状态
- angular - 获取 Angular 9 中所有当前注册组件的列表
- node.js - 在 nextJS (next.config.js) 中设置 webpack 配置
- python-3.x - 从服务主体凭据连接时,azure python sdk 抛出 SSL 握手错误
- flutter - 像工厂一样使用 Provider