Problem: You want to perform normal text operations (such as remove, search, and replace) on a byte string.
Solution Solutions
1) byte strings also support most of the built-in operations like text strings. Like what:
>>> data = B ' Hello world ' >>> data[0:5]b ' hello ' >>> data.startswith (b ' Hello ') true>> > Data.split () [B ' Hello ', b ' World ']>>> data.replace (b ' Hello ', b ' Hello cruel ') b ' Hello Cruel world '
2) These operations also apply to byte arrays. Like what:
>>> data = ByteArray (b ' Hello World ') >>> Data[0:5]bytearray (b ' Hello ') >>> Data.startswith (b ' Hello ') true>>> data.split () [ByteArray (b ' Hello '), ByteArray (b ' world ')]>>> data.replace (b ' Hello ') , b ' Hello cruel ') ByteArray (b ' Hello Cruel World ')
3) You can use regular expressions to match byte strings, but the regular expression itself must also be a byte string.
>>> data = B ' foo:bar,spam ' >>> import re>>> re.split (' [:,] ', data) Traceback (most recent call Last): File ' <stdin> ', line 1, in <module> file '/usr/local/lib/python3.3/re.py ', line 191, in Split return _co Mpile (pattern, flags). Split (String, maxsplit) Typeerror:can ' t use a string pattern on a bytes-like object>>> re. Split (b ' [:,] ', data) # Notice:pattern as Bytes[b ' FOO ', b ' BAR ', B ' SPAM ']
4) In most cases, operations on a text string are available for byte strings. There are, however, some different points to be aware of. First, the index operation of the byte string returns an integer instead of a single character. Like what:
>>> a = ' Hello World ' # Text string>>> a[0] ' H ' >>> a[1 "' e ' >>> b = B ' Hello World ' # Byte String>>> b[0]72>>> b[1]101>>>
5) 2nd, byte strings do not provide an aesthetically pleasing string representation, nor can they be printed well, unless they are first decoded to a text string. Like what:
>>> s = B ' Hello World ' >>> print (s) b ' Hello World ' # Observe B ' ... ' >>> print (S.decode (' ASCII ')) Hello World
6) If you want to format a byte string, you must first use the standard text string and then encode it as a byte string. Like what:
>>> ' {: 10s} {: 10d} {: 10.2f} '. Format (' Acme ', +, 490.1). Encode (' ASCII ') B ' Acme 490.10 ' >>>
7) Finally, it is important to note that the use of byte strings may alter the semantics of some operations, especially those related to file systems. For example, if you use a file name encoded as a byte instead of a plain text string, the encoding/decoding of the file name is disabled. Like what:
8) Finally, some programmers tend to use byte strings rather than text strings in order to increase the speed at which the program executes. Although manipulating byte strings is indeed more efficient than text (because of the Unicode-related overhead of handling text). Doing so often leads to very messy code. You will often find that byte strings do not work well with other parts of Python, and you have to handle all encoding/decoding operations manually. Frankly, if you are working with text, you can use normal text strings instead of byte strings in your program.
String manipulation on a python byte string