Looks like StringIO will work much better here. There's not a great way to empty a bytearray and then refill it with new output. With bytearray, you also have to worry about conversion back and forth from unicode. Conversely, with StringIO, you can treat it like a file. `output.getvalue()` gives you the contents, and then `output.truncate` followed by `output.write` modifies it. I'll have something shortly.