I am writing my own program using peek and this is what I have:
with open('temp.txt', 'r') as inpf2:
while True:
c = [inpf2.read(1)]
if not c:
break
k = c.peek(2)
for d in k:
if(d == ""):
break
else:
c = c.append(d)
print c
In this program, i am using peek to see ahead 2 characters in a text file which has text like abcdefg and trying to print out strings of 3 characters like abc, bcd, cde, def, efg.
But when I execute this program, i am getting the error,
k = c.peek(2)
AttributeError: 'list' object has no attribute 'peek'.
Where am I doing a mistake?
I’m not aware of any Python object having a
peekmethod (apart fromio.BufferedStream, that’s the only method returned from a search in the docs).I’m afraid you’d have to define a
peekfunction yourself. You can find a recipe online that could get you started.For your particular problem, maybe you could consider using
collections.deque