2014-02-10 55 views
0

我想读取文件并执行下面的一些字符串函数。Python 3.3:打印中的TabError

ins = open("data.txt", "r") 
for line in ins: 
category = (line.split("/"))[0] 
print (category) 
ins.close() 

然而,错误信息如下所示:

File "C:\Python33\parsing_food.py", line 4 
print (category) 
      ^
TabError: inconsistent use of tabs and spaces in indentation 

如何解决这个问题?

我想用“/”字符拆分字符串并打印返回数组中的第一个字符串。

+1

您是否尝试确保您的源代码仅使用空格或制表符? –

+0

其语法错误 – MONTYHS

+0

Ignacio Vazquez-Abrams,你对,我修好了! – user3217766

回答

1

错误消息说明了这一切。在这条线:

print (category) 

你缩进了标签。你在其他地方使用过空间。保持一致,你不会有这个错误。

在Python中,代码块使用缩进来表示。空格优于制表符。您应该配置编辑器,当你按下标签

1

更改使用四个空格:

ins = open("data.txt", "r") 
for line in ins: 
category = (line.split("/"))[0] 
print category 
ins.close() 

到:

ins = open("data.txt", "r") 
for line in ins: 
    category = (line.split("/"))[0] 
    print category 
ins.close() 

在Python - 它是所有关于压痕和空间 - 就像错误说。

1

您需要缩进for循环中的行。

for line in ins: 
    category = (line.split("/"))[0] 
    print(category)