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 you convert a byte to a string in Python?Different ways to convert Bytes to string in Python:. Using decode() method.. Using str() function.. Using codecs. decode() method.. Using map() without using the b prefix.. Using pandas to convert bytes to strings.. How would you convert bytes to string?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.
How do you decode bytes in Python?decode() is used to decode bytes to a string object. Decoding to a string object depends on the specified arguments. It also allows us to mention an error handling scheme to use for seconding errors.
How do you write bytes in a text file?Use open() and file.
Open a file for writing in binary mode using open(file, mode) with file as the file name and mode as "wb" . Use file. write(text) with file as the opened file and text as the bytes to write data to a file. Once finished, close the file using file.
|