2014-02-10 52 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)