0

I am trying to make my code narrow down words in a listing an input then sorting them into a different list but it threw out this can anyone help me?

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()
Dan Hirst
  • 13
  • 1
  • 4
  • What do you expect `words[x]` to do? – Moses Koledoye Jul 31 '16 at 23:06
  • Try using `zip()` or `next(iterator)` to iterate through multiple elements (chunks) in an iterator type list. See https://stackoverflow.com/questions/16789776/iterating-over-two-values-of-a-list-at-a-time-in-python – Mr-IDE Mar 03 '18 at 16:17

1 Answers1

2

You problem is that you can't just flat out say words[0] when all you assigned words to is open(filename) the open function in python does not return a list(as you seem to think) instead is returns a file object as said in the python docs about what the open() function does:

Open a file, returning an object of the file type

instead do either words = open(filename).read() or words = list(words) and then you can do words[0]

Christian Dean
  • 22,138
  • 7
  • 54
  • 87