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.
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:
sys.getfilesystemencoding() functionYou can then transform your Unicode variable into a bytes string via var.encode(encoding).
Also check out the Python Unicode HOWTO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With