How do you convert bytes to string in python?
If you don't know the encoding, then to read binary input into string in Python 3 and Python 2 compatible way, use the ancient MS-DOS CP437 encoding: Show
Because encoding is unknown, expect non-English symbols to translate to characters of Decoding arbitrary binary input to UTF-8 is unsafe, because you may get this:
The same applies to UPDATE 20150604: There are rumors that Python 3 has the UPDATE 20170116: Thanks to comment by Nearoo - there is also a possibility to slash escape all unknown bytes with
See Python’s Unicode Support for details. UPDATE 20170119: I decided to implement slash escaping decode that works for both Python 2 and Python 3. It should be slower than the IntroductionIn this article, we'll take a look at how to convert Bytes to a String in Python. By the end of this article you will have a clear idea of what these types are and how to effectively handle data using them. Depending on the version of Python you're using, this task will differ. Although Python 2 has reached its end of life, many projects still use it, so we'll include both the Python 2 and Python 3 approaches. Convert Bytes to String in Python 3Since Python 3, the old ASCII way of doing things had to go, and Python became completely Unicode. This means that we lost the explicit unicode type: To differentiate these strings from good old bytestrings, we're introduced to a new specifier for them - the
Bytestrings in Python 3 are officially called Convert Bytes to String with decode()Let's take a look at how we can convert bytes to a String, using the built-in
Passing the encoding format, we've decoded the Convert Bytes to String with codecsAlternatively, we can use the built-in
You don't really need to pass in the encoding parameter, though, it is advised to pass it in:
Convert Bytes to String with str()Finally, you can use the
Make sure to provide the encoding argument to
This brings us to encodings once again. If you specify the wrong encoding, the best case is your program crashing because it can't decode the data. For example, if we tried using the
This is even more important given that Python 3 likes to assume Unicode - so if you are working with files or data sources that use an obscure encoding, make sure to pay extra attention. Convert Bytes to String in Python 2In Python 2, a bundle of bytes and a string are practically the same thing - strings are objects consisting of 1-byte long characters, meaning that each character can store 256 values. That's why they are sometimes called bytestrings. This is great when working with byte data - we just load it into a variable and we are ready to print:
Using Unicode characters in bytestrings does change this behavior a bit though:
Convert Bytes to Unicode (Python 2)Here, we'll have to use Python 2's The
As you can see above, the Unicode string contains
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! You can get the Unicode string by decoding your bytestring. This can be done by constructing a Unicode object, providing the bytestring and a string containing the encoding name as arguments or by calling Convert Bytes to String Using decode() (Python 2)You can also use the
Convert Bytes to String Using codecs (Python 2)Or, using the
Be Mindful of your EncodingA word of caution here - bytes can be interpreted differently in different encodings. With around 80 different encodings available out of the box, it might not be easy to know if you've got the right one!
The original message was either ConclusionAs programmers, there are some things we must constantly think about and actively prepare for in order to avoid pitfalls. This holds especially true on the lower levels, where we seldom go when we use a high-level language like Python as our daily driver. Things like charsets, encodings and binary are there to remind us that our job is to code - to encode our thoughts into working solutions. Thankfully, a lot of this thinking becomes part of our routine after a few rounds at the keyboard. In this article, we've gone over how to convert bytes to Strings in Python. How do I convert bytes to strings?One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable. The simplest way to do so is using valueOf() method of String class in java.
Which Python function is used to convert bytes object to a string?Convert Bytes to String with str()
Finally, you can use the str() function, which accepts various values and converts them into strings: >>> b = b'Lets grab a \xf0\x9f\x8d\x95!
How many bytes is a string in Python?Note that every string in Python takes additional 49-80 bytes of memory, where it stores supplementary information, such as hash, length, length in bytes, encoding type and string flags. That's why an empty string takes 49 bytes of memory.
How do you decode bytes in Python?Python bytes decode() function is used to convert bytes to string object. Both these functions allow us to specify the error handling scheme to use for encoding/decoding errors. The default is 'strict' meaning that encoding errors raise a UnicodeEncodeError.
|