Encode to bytes before writing (for py3)