4

I need to encode a string of text before using it, but the only problem is it is a variable and all I know is that normally I would use b'string'. I assume I would use variable.encode(), but what encoding would I use?

EDIT: Sorry, I misunderstood the issue and Christian's answer helped me realize what was going on. Thanks to everyone for helping.

diodepain
  • 39
  • 1
  • 8

2 Answers2

3

Well, you have to decide which encoding to use. You can either go with UTF-8 as a sensible default, or infer the encoding from the environment.

The environment can be, for example:

You can then transform your Unicode variable into a bytes string via var.encode(encoding).

Also check out the Python Unicode HOWTO.

Christian Aichinger
  • 6,989
  • 4
  • 40
  • 60
  • Oh thank you, I completely misunderstood the issue I had and this fixed it. I'll mark this as the accepted answer ASAP. – diodepain Apr 15 '14 at 01:02
1

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.

Community
  • 1
  • 1
agconti
  • 17,780
  • 15
  • 80
  • 114
  • Well that's odd, because I'm having serious problems in my Flask application as a result. When I try to use bcrypt without adding the b in front of the string, I get a server 500 error. Obviously, this gives me a problem when using bcrypt on input given by the user. The error I'm getting in Flask says "TypeError: Unicode-objects must be encoded before hashing" – diodepain Apr 15 '14 at 01:00
  • @Zaphod flask expects a utf-8 for its parameters. please see my updated answer – agconti Apr 15 '14 at 01:06