2016-07-31 349 views
0

我想讓我的代碼縮小列表中的單詞輸入然後將它們排序到不同的列表,但它扔出來這可以幫助我?當創建腳本有:TypeError:'_io.TextIOWrapper'對象是不可訂

Traceback (most recent call last): 
File "C:/Users/dan/Desktop/python/threeword.py", line 4, in <module> 
word = words[x],words[(x+1)],words[(x+2)] 
TypeError: '_io.TextIOWrapper' object is not subscriptable 

words=open("three.txt",'r+') 
f=open("three1","w") 
for x in words: 
    word = words[x],words[(x+1)],words[(x+2)] 
    print(word) 
    input=('y or n') 
    if input=="y": 
     f.write(word) 
     x=x+3 
    elif input=='stop': 
     break 
    else: 
     x=x+3 
f.close() 
+0

你期望'單詞[x]'做什麼? –

+0

嘗試使用'zip()'或'next(iterator)'在迭代器類型列表中遍歷多個元素(塊)。見https://stackoverflow.com/questions/16789776/iterating-over-two-values-of-a-list-at-a-time-in-python –

回答

1

您的問題是,你不能只是平了說words[0]當你分配給wordsopen(filename)在Python沒有返回一個列表(你似乎認爲)反而是open函數返回一個file對象在有關open()的功能是什麼Python文檔的說:

Open a file, returning an object of the file type

,而不是做任何 words = open(filename).read()words = list(words),然後你可以做words[0]

〜Mr.Python

+0

謝謝你工作:) –

相關問題