For example, I have a string like this(return value of subprocess.check_output):
>>> b'a string'
b'a string'
Whatever I did to it, it is always printed with the annoying b' before the string:
>>> print(b'a string')
b'a string'
>>> print(str(b'a string'))
b'a string'
Does anyone have any ideas about how to use it as a normal string or convert it into a normal string?
asked Jul 12, 2013 at 12:55
Hanfei Sun
47.3k42 gold badges136 silver badges253 bronze badges
-
1@HanfeiSun what you call a "binary string" is a bytes object (see information about bytes object in the standard library )loved.by.Jesus– loved.by.Jesus2020年01月06日 13:47:40 +00:00Commented Jan 6, 2020 at 13:47
3 Answers 3
Decode it.
>>> b'a string'.decode('ascii')
'a string'
To get bytes from string, encode it.
>>> 'a string'.encode('ascii')
b'a string'
answered Jul 12, 2013 at 12:55
falsetru
371k69 gold badges770 silver badges660 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
falsetru
@lyomi, I used
ascii because the given string was made with ascii letters. You don't need to specify encoding if the encoding is utf-8 (default in Python 3.x according to str.encode, bytes.decode doc-string)Jmons
@lyomi In 2016 (and its nearly the end) people still use ascii. There are many many 'legacy' products and systems (including specifications), but there are also lots of reasons why you might be creating a 'binary string' where you don't want unicode or something to try and 'merge' multiple bytes into a single character. We often use 'strings' to contain binary data for instance making DNS requests etc.
aturegano
I suggest to add the following to complete the answer. Most times we need to decode bytes from our operating system, such as console output, the most pythonic way I found to do it is to
import locale and then os_encoding = locale.getpreferredencoding(). This way, we can decode using my_b_string.decode(os_encoding)falsetru
@aturegano, It's not the only option.
sys.getfilesystemencoding(), sys.stdin.encoding, sys.stdout.encoding. IMHO, using those automatic encoding detection could solve problem because the sub-program (OP is using subprocess) could be written other way to determine encoding (or even hard-coded). Thanks for feedback, anyway.aturegano
@falsetru Note that
sys.getfilesystemencoding() returns the name of the encoding used to convert between Unicode filenames and bytes filenames and is strongly dependant on operating system you are using. AFAIK, this function is used to convert to the system’s preferred representation. That means that it will not infer the codification used by the console that can be obtained using the aforementioned locale.getpreferredencoding() function |
See the official encode() and decode() documentation from codecs library. utf-8 is the default encoding for the functions, but there are severals standard encodings in Python 3, like latin_1 or utf_32.
joel
8,1325 gold badges43 silver badges75 bronze badges
answered Jun 2, 2020 at 17:52
Daniel Argüelles
2,3371 gold badge34 silver badges57 bronze badges
1 Comment
Tom Renish
This post is helpful in that it remedies error messages like this, "'utf-8' codec can't decode byte..."
lang-py