444

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?

1

3 Answers 3

634

Decode it.

>>> b'a string'.decode('ascii')
'a string'

To get bytes from string, encode it.

>>> 'a string'.encode('ascii')
b'a string'
Sign up to request clarification or add additional context in comments.

7 Comments

@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)
@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.
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)
@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.
@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
|
130

If the answer from falsetru didn't work you could also try:

>>> b'a string'.decode('utf-8')
'a string'

Comments

13

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.

1 Comment

This post is helpful in that it remedies error messages like this, "'utf-8' codec can't decode byte..."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.