150

I have read in an XML email attachment with

bytes_string=part.get_payload(decode=False)

The payload comes in as a byte string, as my variable name suggests.

I am trying to use the recommended Python 3 approach to turn this string into a usable string that I can manipulate.

The example shows:

str(b'abc','utf-8')

How can I apply the b (bytes) keyword argument to my variable bytes_string and use the recommended approach?

The way I tried doesn't work:

str(bbytes_string, 'utf-8')
1

4 Answers 4

251

You had it nearly right in the last line. You want

str(bytes_string, 'utf-8')

because the type of bytes_string is bytes, the same as the type of b'abc'.

4
  • 13
    str(bytes_string, 'utf-8', 'ignore') Errors can be ignored by passing the third parameter.
    – Shubhamoy
    Commented Jun 8, 2018 at 6:14
  • 3
    That looks like it should be a comment to pylang's answer (which addresses handling invalid input). If (you believe that) there's nothing wrong with bytes_string, why would you want to ignore errors? Commented Jun 18, 2018 at 8:36
  • 5
    I am getting following error with your approach: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte for the following bytes string b'\xbf\x8cd\xba\x7f\xe0\xf0\xb8t\xfe.TaFJ\xad\x100\x07p\xa0\x1f90\xb7P\x8eP\x90\x06)0' @TobySpeight
    – alper
    Commented Feb 28, 2019 at 8:41
  • Well @alper, that's not a valid UTF-8 string, so what did you expect? Commented Feb 28, 2019 at 9:58
58

Call decode() on a bytes instance to get the text which it encodes.

str = bytes.decode()
4
  • 7
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 230: invalid start byte Commented Jun 6, 2016 at 13:03
  • 3
    @JuhaUntinen your encoding is probably not utf-8. Commented Sep 22, 2016 at 15:29
  • 4
    How to filter (skip) non-UTF8 charachers from array?
    – Dr. Failov
    Commented Dec 21, 2016 at 21:36
  • Use str = bytes.decode("utf-8) to use a diffirent encoding. Replace utf-8 to the encoding you want. Commented Jun 2, 2021 at 4:44
13

How to filter (skip) non-UTF8 charachers from array?

To address this comment in @uname01's post and the OP, ignore the errors:

Code

>>> b'\x80abc'.decode("utf-8", errors="ignore")
'abc'

Details

From the docs, here are more examples using the same errors parameter:

>>> b'\x80abc'.decode("utf-8", "replace")
'\ufffdabc'
>>> b'\x80abc'.decode("utf-8", "backslashreplace")
'\\x80abc'
>>> b'\x80abc'.decode("utf-8", "strict")  
Traceback (most recent call last):
    ...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0:
  invalid start byte

The errors argument specifies the response when the input string can’t be converted according to the encoding’s rules. Legal values for this argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' (use U+FFFD, REPLACEMENT CHARACTER), or 'ignore' (just leave the character out of the Unicode result).

12

UPDATED:

TO NOT HAVE ANY b and quotes at first and end

How to convert bytes as seen to strings, even in weird situations.

As your code may have unrecognizable characters to 'utf-8' encoding, it's better to use just str without any additional parameters:

some_bad_bytes = b'\x02-\xdfI#)'
text = str( some_bad_bytes )[2:-1]

print(text)
Output: \x02-\xdfI

if you add 'utf-8' parameter, to these specific bytes, you should receive error.

As PYTHON 3 standard says, text would be in utf-8 now with no concern.

4
  • result is "b'\\x02-\\xdfI#)'" which probably isn't what he wants Commented Oct 18, 2017 at 20:49
  • @GlenThompson it is just an example for unwanted conditions, that may happen. I use this specific text intentionally. If you mean text has a b in first of it, then I updated answer
    – Seyfi
    Commented Oct 19, 2017 at 20:46
  • so very thanks i'm searching for a way for remove the b'' of an string that have ansi character without encoding and lossing the characters, i'm new in python and don't know than i can reduce an array from start and beginning using indexes :O Commented Feb 9, 2018 at 20:17
  • @DiegoFernandoMurilloValenci , your welcome. Glad to I can help.
    – Seyfi
    Commented Mar 1, 2018 at 20:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.