c = 'can\nhaz\nfoo'
sio = StringIO()
sio.write(c)
sio.seek(0)
offset = sio.tell()
for line in sio:
    if line == 'haz\n':
        sio.seek(offset)
sio.write('bar\n')
break
offset = sio.tell()
in-place replacement in StringIO