I have a file consisting of words, one word on each line. The file looks like this:
for line in content:
print line, next(line);
TypeError: str object is not an iterator
for x in it:
print x, next(x);
x holds a string 'ddd/ccc/etc'. it has not next.
next() belongs to the iterator and it used to get next element from the iterator. The correct way to call it is
it=iter(content) for x in it: print x, it.next();
But you will get an exception after you finish to consume all elements in the iterator. So, you need to catch StopIteration exception.
it=iter(content) for x in it: try: line, next_line = x, it.next() #do your counting logic here except StopIteration: break