You dont have to do anything to encode a variable like b'string'
because its ignored by python 2.7
. Its used to encode strings as bytes in python 3.3
.
I learned this from great answer by NPE which explains this in more detail:
To quote the python 2.x documentation:
A prefix of 'b' or 'B' is ignored in
Python 2; it indicates that the
literal should become a bytes literal
in Python 3 (e.g. when code is
automatically converted with 2to3). A
'u' or 'b' prefix may be followed by
an 'r' prefix.
The python 3.3 documentation states:
Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only
contain ASCII characters; bytes with a numeric value of 128 or greater
must be expressed with escapes.
The flask error is an issue caused becuase the string is not encoded in UTF-8.
Encode it like so:
your_string = your_string.encode('utf8')
For more details on this issue, look at the reported github issue here.